1use std::sync::Arc;
2
3use chrono::{DateTime, Utc};
4
5use crate::config::settings::Settings;
6use crate::store::db::Database;
7use crate::store::entities::{EntitySearchResult, EntityType};
8use crate::store::memory::{GraphData, Memory, MemoryStats, ProjectSummary, SearchQuery, Session};
9
10#[derive(Debug, Clone, Copy, PartialEq)]
12pub enum DetailTab {
13 Content,
14 Structured,
15 Entities,
16 Temporal,
17 Relations,
18}
19impl DetailTab {
20 pub fn label(&self) -> &'static str {
21 match self {
22 Self::Content => "Content",
23 Self::Structured => "Fields",
24 Self::Entities => "Entities",
25 Self::Temporal => "Temporal",
26 Self::Relations => "Graph",
27 }
28 }
29 pub fn next(&self) -> Self {
30 match self {
31 Self::Content => Self::Structured,
32 Self::Structured => Self::Entities,
33 Self::Entities => Self::Temporal,
34 Self::Temporal => Self::Relations,
35 Self::Relations => Self::Content,
36 }
37 }
38 pub fn prev(&self) -> Self {
39 match self {
40 Self::Content => Self::Relations,
41 Self::Structured => Self::Content,
42 Self::Entities => Self::Structured,
43 Self::Temporal => Self::Entities,
44 Self::Relations => Self::Temporal,
45 }
46 }
47}
48
49pub struct App {
51 pub project: String,
52 pub memories: Vec<Memory>,
53 pub sessions: Vec<Session>,
54 pub selected: usize,
55 pub scroll: usize,
56 pub search: String,
57 pub status_msg: Option<String>,
58 pub quit: bool,
59 pub show_help: bool,
60 pub detail_tab: DetailTab,
61 pub detail_scroll: usize,
62 pub stats: Option<MemoryStats>,
63 pub graph: Option<GraphData>,
64 pub graph_sel: usize,
65 pub entity_data: Option<Vec<(String, EntityType, u32)>>,
66 pub temporal_data: Option<(Vec<Memory>, u8)>,
67 pub active_panel: usize, pub total_mems: u32,
69 pub db: Arc<Database>,
70}
71
72impl App {
73 pub fn new(db: Arc<Database>, _settings: Arc<Settings>) -> Self {
74 let project = Settings::infer_project();
75 Self {
76 project,
77 memories: Vec::new(),
78 sessions: Vec::new(),
79 selected: 0,
80 scroll: 0,
81 search: String::new(),
82 status_msg: None,
83 quit: false,
84 show_help: false,
85 detail_tab: DetailTab::Content,
86 detail_scroll: 0,
87 stats: None,
88 graph: None,
89 graph_sel: 0,
90 entity_data: None,
91 temporal_data: None,
92 active_panel: 0,
93 total_mems: 0,
94 db,
95 }
96 }
97
98 pub fn load(&mut self) {
100 let s = self.db.memories();
101 if self.search.is_empty() {
102 self.memories = s
103 .list(&self.project, None, None, None, 500, 0)
104 .unwrap_or_default();
105 } else {
106 let q = SearchQuery {
107 text: self.search.clone(),
108 project: Some(self.project.clone()),
109 scope: None,
110 memory_type: None,
111 importance: None,
112 tags: vec![],
113 limit: 200,
114 include_snippet: false,
115 all_projects: false,
116 };
117 self.memories = s
118 .search(&q, &crate::store::search::SearchWeights::default(), None)
119 .unwrap_or_default()
120 .into_iter()
121 .map(|r| r.memory)
122 .collect();
123 }
124 self.selected = self.selected.min(self.memories.len().saturating_sub(1));
125 self.scroll = 0;
126 self.detail_scroll = 0;
127 self.stats = s.stats(&self.project).ok();
128 self.total_mems = self.stats.as_ref().map(|s| s.total_memories).unwrap_or(0);
129 self.sessions = self
130 .db
131 .sessions()
132 .list(&self.project, 50)
133 .unwrap_or_default();
134 }
135
136 pub fn down(&mut self) {
138 if !self.memories.is_empty() {
139 self.selected = (self.selected + 1).min(self.memories.len() - 1);
140 if self.selected >= self.scroll + 20 {
141 self.scroll += 1;
142 }
143 }
144 }
145 pub fn up(&mut self) {
146 self.selected = self.selected.saturating_sub(1);
147 if self.selected < self.scroll {
148 self.scroll = self.scroll.saturating_sub(1);
149 }
150 }
151 pub fn first(&mut self) {
152 self.selected = 0;
153 self.scroll = 0;
154 }
155 pub fn last(&mut self) {
156 if !self.memories.is_empty() {
157 self.selected = self.memories.len() - 1;
158 self.scroll = self.selected.saturating_sub(19);
159 }
160 }
161 pub fn pgdn(&mut self) {
162 if !self.memories.is_empty() {
163 self.selected = (self.selected + 20).min(self.memories.len() - 1);
164 if self.selected >= self.scroll + 20 {
165 self.scroll += 20;
166 }
167 }
168 }
169 pub fn pgup(&mut self) {
170 self.selected = self.selected.saturating_sub(20);
171 if self.selected < self.scroll {
172 self.scroll = self.scroll.saturating_sub(20);
173 }
174 }
175 pub fn sel(&self) -> Option<&Memory> {
176 self.memories.get(self.selected)
177 }
178
179 pub fn tab_next(&mut self) {
181 self.detail_tab = self.detail_tab.next();
182 self.detail_scroll = 0;
183 }
184 pub fn tab_prev(&mut self) {
185 self.detail_tab = self.detail_tab.prev();
186 self.detail_scroll = 0;
187 }
188 pub fn dscroll_down(&mut self) {
189 self.detail_scroll += 3;
190 }
191 pub fn dscroll_up(&mut self) {
192 self.detail_scroll = self.detail_scroll.saturating_sub(3);
193 }
194
195 pub fn delete_sel(&mut self) {
197 if let Some(m) = self.sel() {
198 self.db.memories().delete(m.id, false).ok();
199 self.status_msg = Some("🗑 Deleted".into());
200 self.load();
201 }
202 }
203 pub fn load_graph(&mut self) {
204 self.graph = self.db.memories().get_graph(&self.project).ok();
205 self.graph_sel = 0;
206 }
207 pub fn graph_next(&mut self) {
208 if let Some(ref d) = self.graph {
209 if !d.nodes.is_empty() {
210 self.graph_sel = (self.graph_sel + 1) % d.nodes.len();
211 }
212 }
213 }
214 pub fn graph_prev(&mut self) {
215 if let Some(ref d) = self.graph {
216 if !d.nodes.is_empty() {
217 self.graph_sel = self.graph_sel.checked_sub(1).unwrap_or(d.nodes.len() - 1);
218 }
219 }
220 }
221 pub fn load_entity(&mut self) {
222 self.entity_data = self.db.entities().frequent_entities(&self.project, 30).ok();
223 }
224 pub fn load_temporal(&mut self) {
225 self.temporal_data = Some((
226 self.db
227 .memories()
228 .list(&self.project, None, None, None, 500, 0)
229 .unwrap_or_default(),
230 0,
231 ));
232 }
233 pub fn temporal_cycle(&mut self) {
234 if let Some(ref mut td) = self.temporal_data {
235 td.1 = (td.1 + 1) % 3;
236 }
237 }
238}