1use anyhow::Result;
2use chrono::{DateTime, Utc};
3use serde::{Deserialize, Serialize};
4use std::path::{Path, PathBuf};
5use uuid::Uuid;
6
7use crate::compress::CompressionHistoryEntry;
8use crate::providers::Message;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct SessionMetadata {
13 pub id: String,
15 pub name: Option<String>,
17 pub project_path: Option<String>,
19 pub created_at: DateTime<Utc>,
21 pub updated_at: DateTime<Utc>,
23 pub message_count: usize,
25 pub last_input_tokens: u64,
27 pub total_output_tokens: u64,
29 #[serde(default, skip_serializing_if = "Vec::is_empty")]
31 pub compression_history: Vec<CompressionHistoryEntry>,
32}
33
34impl SessionMetadata {
35 pub fn new(project_path: Option<&Path>) -> Self {
37 let now = Utc::now();
38 Self {
39 id: Uuid::new_v4().to_string(),
40 name: None, project_path: project_path.map(|p| p.to_string_lossy().to_string()),
42 created_at: now,
43 updated_at: now,
44 message_count: 0,
45 last_input_tokens: 0,
46 total_output_tokens: 0,
47 compression_history: Vec::new(),
48 }
49 }
50
51 fn generate_time_name(time: DateTime<Utc>) -> String {
54 let local: chrono::DateTime<chrono::Local> = time.with_timezone(&chrono::Local);
56 local.format("%Y-%m-%d %H:%M").to_string()
57 }
58
59 pub fn add_compression_entry(&mut self, entry: CompressionHistoryEntry) {
61 self.compression_history.push(entry);
62 if self.compression_history.len() > 10 {
64 self.compression_history.remove(0);
65 }
66 }
67
68 pub fn total_tokens_saved(&self) -> u32 {
70 self.compression_history
71 .iter()
72 .map(|e| e.tokens_saved)
73 .sum()
74 }
75
76 pub fn compression_count(&self) -> usize {
78 self.compression_history.len()
79 }
80
81 pub fn display_name(&self) -> String {
84 if let Some(ref name) = self.name {
85 name.clone()
86 } else {
87 Self::generate_time_name(self.created_at)
89 }
90 }
91
92 pub fn short_id(&self) -> String {
94 self.id[..8].to_string()
95 }
96
97 pub fn format_line(&self, is_current: bool) -> String {
99 let marker = if is_current { "*" } else { " " };
100 let name = self.display_name();
101 let msgs = self.message_count;
102 let project = self
103 .project_path
104 .as_ref()
105 .map(|p| {
106 PathBuf::from(p)
108 .file_name()
109 .map(|n| n.to_string_lossy().to_string())
110 .unwrap_or_else(|| p.clone())
111 })
112 .unwrap_or_else(|| "-".to_string());
113
114 let compression_info = if self.compression_count() > 0 {
116 format!(" 💾 {} comps", self.compression_count())
117 } else {
118 "".to_string()
119 };
120
121 format!(
122 "{} {} {} msgs {}{}",
123 marker, name, msgs, project, compression_info
124 )
125 }
126}
127
128#[derive(Debug, Clone, Serialize, Deserialize, Default)]
130pub struct SessionIndex {
131 pub sessions: Vec<SessionMetadata>,
133 pub last_session_id: Option<String>,
135}
136
137impl SessionIndex {
138 pub fn find(&self, query: &str) -> Option<&SessionMetadata> {
140 if let Some(s) = self.sessions.iter().find(|s| s.id == query) {
142 return Some(s);
143 }
144 if let Some(s) = self
146 .sessions
147 .iter()
148 .find(|s| s.name.as_deref() == Some(query))
149 {
150 return Some(s);
151 }
152 if let Some(s) = self.sessions.iter().find(|s| s.id.starts_with(query)) {
154 return Some(s);
155 }
156 None
157 }
158
159 pub fn last_session(&self) -> Option<&SessionMetadata> {
161 self.last_session_id
162 .as_ref()
163 .and_then(|id| self.sessions.iter().find(|s| s.id == *id))
164 }
165
166 pub fn upsert(&mut self, meta: SessionMetadata) {
168 self.sessions.retain(|s| s.id != meta.id);
170 self.sessions.push(meta.clone());
172 self.last_session_id = Some(meta.id);
174 self.sessions
176 .sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
177 }
178
179 pub fn remove(&mut self, id: &str) -> Option<SessionMetadata> {
181 let removed = self.sessions.iter().position(|s| s.id == id);
182 if let Some(idx) = removed {
183 let meta = self.sessions.remove(idx);
184 if self.last_session_id.as_deref() == Some(id) {
185 self.last_session_id = self.sessions.first().map(|s| s.id.clone());
186 }
187 Some(meta)
188 } else {
189 None
190 }
191 }
192
193 pub fn rename(&mut self, id: &str, new_name: &str) -> Result<()> {
195 let session = self.sessions.iter_mut().find(|s| s.id == id);
196 if let Some(s) = session {
197 s.name = Some(new_name.to_string());
198 s.updated_at = Utc::now();
199 Ok(())
200 } else {
201 anyhow::bail!("session {} not found", id)
202 }
203 }
204}
205
206#[derive(Debug, Clone, Serialize, Deserialize)]
209pub struct MessageSummary {
210 pub role: String,
212 pub preview: String,
214 #[serde(skip_serializing_if = "Option::is_none")]
216 pub timestamp: Option<DateTime<Utc>>,
217 pub is_compressed: bool,
219 pub original_index: usize,
221}
222
223impl MessageSummary {
224 pub fn from_message(msg: &Message, index: usize) -> Self {
226 use crate::providers::{ContentBlock, MessageContent, Role};
227 use crate::truncate::truncate_chars;
228
229 let role = match msg.role {
230 Role::User => "user",
231 Role::Assistant => "assistant",
232 Role::Tool => "tool",
233 Role::System => "system",
234 };
235
236 let preview = match &msg.content {
237 MessageContent::Text(t) => truncate_chars(t, 100),
238 MessageContent::Blocks(blocks) => {
239 let parts: Vec<String> = blocks
240 .iter()
241 .take(3)
242 .map(|b| match b {
243 ContentBlock::Text { text } => truncate_chars(text, 50),
244 ContentBlock::ToolUse { name, .. } => format!("[{}]", name),
245 ContentBlock::ToolResult { content, .. } => truncate_chars(content, 50),
246 ContentBlock::Thinking { thinking, .. } => format!("💭 {}", truncate_chars(thinking, 30)),
247 _ => "...".to_string(),
248 })
249 .collect();
250 parts.join(" ")
251 }
252 };
253
254 Self {
255 role: role.to_string(),
256 preview,
257 timestamp: None,
258 is_compressed: false,
259 original_index: index,
260 }
261 }
262}
263
264#[derive(Debug, Clone, Serialize, Deserialize)]
266pub struct Session {
267 pub metadata: SessionMetadata,
268 #[serde(default)]
270 pub full_messages: Vec<Message>,
271 #[serde(default)]
274 pub compressed_messages: Vec<Message>,
275 #[serde(default)]
277 pub message_summaries: Vec<MessageSummary>,
278 #[serde(default, skip_serializing)]
280 pub messages: Vec<Message>,
281}
282
283impl Session {
284 pub fn new(project_path: Option<&Path>) -> Self {
286 Self {
287 metadata: SessionMetadata::new(project_path),
288 full_messages: Vec::new(),
289 compressed_messages: Vec::new(),
290 message_summaries: Vec::new(),
291 messages: Vec::new(),
292 }
293 }
294
295 pub fn from_messages(messages: Vec<Message>, project_path: Option<&Path>) -> Self {
297 let mut meta = SessionMetadata::new(project_path);
298 meta.message_count = messages.len();
299 Self {
300 metadata: meta,
301 full_messages: messages.clone(),
302 compressed_messages: Vec::new(),
303 message_summaries: messages.iter().enumerate()
304 .map(|(i, m)| MessageSummary::from_message(m, i))
305 .collect(),
306 messages: messages,
307 }
308 }
309
310 pub fn api_messages(&self) -> &[Message] {
312 if self.compressed_messages.is_empty() {
313 &self.full_messages
314 } else {
315 &self.compressed_messages
316 }
317 }
318
319 pub fn display_messages(&self) -> &[Message] {
321 &self.full_messages
322 }
323
324 pub fn update_stats(&mut self, last_input_tokens: u32, total_output_tokens: u64) {
326 self.metadata.message_count = self.full_messages.len();
327 self.metadata.last_input_tokens = last_input_tokens as u64;
328 self.metadata.total_output_tokens = total_output_tokens;
329 self.metadata.updated_at = Utc::now();
330 }
331
332 pub fn set_compressed(&mut self, compressed: Vec<Message>, summaries: Vec<MessageSummary>) {
334 self.compressed_messages = compressed;
335 self.message_summaries = summaries;
336 }
337
338 fn migrate_legacy(&mut self) {
340 if !self.messages.is_empty() && self.full_messages.is_empty() {
341 log::info!(
342 "Migrating legacy session: {} messages -> full_messages",
343 self.messages.len()
344 );
345 self.full_messages = self.messages.clone();
346 self.message_summaries = self.messages.iter().enumerate()
347 .map(|(i, m)| MessageSummary::from_message(m, i))
348 .collect();
349 self.messages.clear();
350 log::info!(
351 "Migration complete: full_messages={}, summaries={}",
352 self.full_messages.len(),
353 self.message_summaries.len()
354 );
355 }
356 }
357}
358
359pub struct SessionManager {
361 base_dir: PathBuf,
363 current_session: Option<Session>,
365 index: SessionIndex,
367}
368
369impl SessionManager {
370 pub fn new() -> Result<Self> {
372 let base_dir = Self::get_base_dir()?;
373 let manager = Self {
374 base_dir,
375 current_session: None,
376 index: SessionIndex::default(),
377 };
378 manager.ensure_dirs()?;
379 let mut manager = manager;
380 manager.load_index()?;
381 Ok(manager)
382 }
383
384 fn get_base_dir() -> Result<PathBuf> {
386 let home = std::env::var_os("HOME")
387 .or_else(|| std::env::var_os("USERPROFILE"))
388 .ok_or_else(|| anyhow::anyhow!("HOME or USERPROFILE environment variable not set"))?;
389 let mut p = PathBuf::from(home);
390 p.push(".matrix");
391 Ok(p)
392 }
393
394 fn sessions_dir(&self) -> PathBuf {
396 self.base_dir.join("sessions")
397 }
398
399 fn index_path(&self) -> PathBuf {
401 self.sessions_dir().join("index.json")
402 }
403
404 fn session_path(&self, id: &str) -> PathBuf {
406 self.sessions_dir().join(format!("{}.json", id))
407 }
408
409 fn ensure_dirs(&self) -> Result<()> {
411 std::fs::create_dir_all(&self.base_dir)
412 .with_context(|| format!("creating base dir {}", self.base_dir.display()))?;
413 std::fs::create_dir_all(self.sessions_dir())
414 .with_context(|| format!("creating sessions dir {}", self.sessions_dir().display()))?;
415 Ok(())
416 }
417
418 fn load_index(&mut self) -> Result<()> {
420 let path = self.index_path();
421 if !path.exists() {
422 return Ok(());
423 }
424 let data = std::fs::read_to_string(&path)
425 .with_context(|| format!("reading index file {}", path.display()))?;
426 if data.trim().is_empty() {
427 return Ok(());
428 }
429 self.index = serde_json::from_str(&data)
430 .with_context(|| format!("parsing index file {}", path.display()))?;
431 Ok(())
432 }
433
434 fn save_index(&self) -> Result<()> {
436 let path = self.index_path();
437 let json =
438 serde_json::to_string_pretty(&self.index).context("serializing session index")?;
439 let tmp = path.with_extension("json.tmp");
440 std::fs::write(&tmp, json)
441 .with_context(|| format!("writing index tmp file {}", tmp.display()))?;
442 std::fs::rename(&tmp, &path)
443 .with_context(|| format!("renaming index tmp file to {}", path.display()))?;
444 Ok(())
445 }
446
447 pub fn start_new(&mut self, project_path: Option<&Path>) -> Result<&Session> {
449 let session = Session::new(project_path);
450 self.current_session = Some(session);
451 self.save_current()?;
452 Ok(self.current_session.as_ref().unwrap())
454 }
455
456 pub fn continue_last(&mut self, project_path: Option<&Path>) -> Result<Option<&Session>> {
458 let last_id = self.index.last_session().map(|m| m.id.clone());
459 if let Some(id) = last_id {
460 self.load_session(&id)?;
461 if let Some(path) = project_path
463 && let Some(ref mut session) = self.current_session
464 {
465 session.metadata.project_path = Some(path.to_string_lossy().to_string());
466 }
467 Ok(self.current_session.as_ref())
468 } else {
469 Ok(None)
470 }
471 }
472
473 pub fn resume(&mut self, query: &str, project_path: Option<&Path>) -> Result<Option<&Session>> {
475 let session_id = self.index.find(query).map(|m| m.id.clone());
476 if let Some(id) = session_id {
477 self.load_session(&id)?;
478 if let Some(path) = project_path
480 && let Some(ref mut session) = self.current_session
481 {
482 session.metadata.project_path = Some(path.to_string_lossy().to_string());
483 }
484 Ok(self.current_session.as_ref())
485 } else {
486 Ok(None)
487 }
488 }
489
490 fn load_session(&mut self, id: &str) -> Result<()> {
492 let path = self.session_path(id);
493 if !path.exists() {
494 anyhow::bail!("session file {} not found", path.display());
495 }
496 let data = std::fs::read_to_string(&path)
497 .with_context(|| format!("reading session file {}", path.display()))?;
498 let mut session: Session = serde_json::from_str(&data)
499 .with_context(|| format!("parsing session file {}", path.display()))?;
500
501 session.migrate_legacy();
503
504 if session.metadata.name.is_none()
506 && let Some(index_meta) = self.index.find(id)
507 {
508 session.metadata.name = index_meta.name.clone();
509 }
510
511 self.current_session = Some(session);
512 Ok(())
513 }
514
515 pub fn save_current(&mut self) -> Result<()> {
517 if let Some(ref session) = self.current_session {
518 self.index.upsert(session.metadata.clone());
520 self.save_index()?;
521
522 let path = self.session_path(&session.metadata.id);
524 let json = serde_json::to_string(session).context("serializing session")?;
525 let tmp = path.with_extension("json.tmp");
526 std::fs::write(&tmp, json)
527 .with_context(|| format!("writing session tmp file {}", tmp.display()))?;
528 std::fs::rename(&tmp, &path)
529 .with_context(|| format!("renaming session tmp file to {}", path.display()))?;
530 }
531 Ok(())
532 }
533
534 pub fn update_stats(&mut self, last_input_tokens: u32, total_output_tokens: u64) {
536 if let Some(ref mut session) = self.current_session {
537 session.update_stats(last_input_tokens, total_output_tokens);
538 }
539 }
540
541 pub fn record_compression(&mut self, entry: crate::compress::CompressionHistoryEntry) {
543 if let Some(ref mut session) = self.current_session {
544 session.metadata.add_compression_entry(entry);
545 }
546 }
547
548 pub fn set_messages(&mut self, messages: Vec<Message>) {
550 if let Some(ref mut session) = self.current_session {
551 if session.metadata.name.is_none()
553 && !messages.is_empty()
554 && let Some(name) = Self::generate_name_from_messages(&messages)
555 {
556 session.metadata.name = Some(name);
557 }
558
559 session.full_messages = messages.clone();
561 session.message_summaries = messages.iter().enumerate()
562 .map(|(i, m)| MessageSummary::from_message(m, i))
563 .collect();
564 session.metadata.message_count = session.full_messages.len();
565 session.metadata.updated_at = Utc::now();
566 }
567 }
568
569 pub fn set_compressed_messages(&mut self, compressed: Vec<Message>) {
571 if let Some(ref mut session) = self.current_session {
572 for summary in &mut session.message_summaries {
574 summary.is_compressed = true;
575 }
576
577 for compressed_msg in &compressed {
580 for (idx, full_msg) in session.full_messages.iter().enumerate() {
581 if session.message_summaries.get(idx).is_some() {
583 let same_role = compressed_msg.role == full_msg.role;
584 if same_role {
585 if let Some(summary) = session.message_summaries.get_mut(idx) {
587 summary.is_compressed = false;
588 }
589 }
590 }
591 }
592 }
593
594 session.compressed_messages = compressed;
595 }
596 }
597
598 pub fn api_messages(&self) -> Option<&[Message]> {
600 self.current_session.as_ref().map(|s| s.api_messages())
601 }
602
603 pub fn display_messages(&self) -> Option<&[Message]> {
605 self.current_session.as_ref().map(|s| s.display_messages())
606 }
607
608 fn generate_name_from_messages(messages: &[Message]) -> Option<String> {
611 use crate::providers::{ContentBlock, MessageContent, Role};
612
613 let user_messages: Vec<&Message> =
615 messages.iter().filter(|m| m.role == Role::User).collect();
616
617 for msg in user_messages.iter().take(3) {
618 let text = match &msg.content {
619 MessageContent::Text(t) => t.clone(),
620 MessageContent::Blocks(blocks) => blocks
621 .iter()
622 .filter_map(|b| {
623 if let ContentBlock::Text { text } = b {
624 Some(text.clone())
625 } else {
626 None
627 }
628 })
629 .collect::<Vec<_>>()
630 .join(" "),
631 };
632
633 let cleaned = text.trim().lines().next().unwrap_or("").trim();
634
635 if cleaned.len() < 5 || is_generic_message(cleaned) {
637 continue;
638 }
639
640 let name = if cleaned.chars().count() > 40 {
642 let truncated: String = cleaned.chars().take(37).collect();
643 format!("{}...", truncated)
644 } else {
645 cleaned.to_string()
646 };
647
648 return Some(name);
649 }
650
651 None
652 }
653
654 pub fn messages(&self) -> Option<&[Message]> {
656 self.current_session.as_ref().map(|s| s.api_messages())
657 }
658
659 pub fn messages_mut(&mut self) -> Option<&mut Vec<Message>> {
661 self.current_session.as_mut().map(|s| &mut s.full_messages)
662 }
663
664 pub fn full_messages(&self) -> Option<&[Message]> {
666 self.current_session.as_ref().map(|s| s.display_messages())
667 }
668
669 pub fn current_id(&self) -> Option<&str> {
671 self.current_session
672 .as_ref()
673 .map(|s| s.metadata.id.as_str())
674 }
675
676 pub fn current_name(&self) -> Option<&str> {
678 self.current_session.as_ref().and_then(|s| s.name())
679 }
680
681 pub fn rename_current(&mut self, new_name: &str) -> Result<()> {
683 if let Some(ref session) = self.current_session {
684 let id = session.metadata.id.clone();
685 self.index.rename(&id, new_name)?;
686 if let Some(ref mut session) = self.current_session {
687 session.metadata.name = Some(new_name.to_string());
688 }
689 self.save_current()?;
690 }
691 Ok(())
692 }
693
694 pub fn clear_current(&mut self) -> Result<()> {
696 if let Some(ref session) = self.current_session {
697 let path = self.session_path(&session.metadata.id);
699 let _ = std::fs::remove_file(&path);
700 self.index.remove(&session.metadata.id);
702 self.save_index()?;
703 }
704 self.current_session = None;
705 Ok(())
706 }
707
708 pub fn list_sessions(&self) -> &[SessionMetadata] {
710 &self.index.sessions
711 }
712
713 pub fn has_current(&self) -> bool {
715 self.current_session.is_some()
716 }
717
718 pub fn current_metadata(&self) -> Option<&SessionMetadata> {
720 self.current_session.as_ref().map(|s| &s.metadata)
721 }
722
723 pub fn history_path(&self) -> PathBuf {
725 self.base_dir.join("history.txt")
726 }
727}
728
729impl Session {
730 pub fn name(&self) -> Option<&str> {
732 self.metadata.name.as_deref()
733 }
734}
735
736use anyhow::Context;
737
738fn is_generic_message(msg: &str) -> bool {
740 let generic = [
741 "继续", "好的", "ok", "yes", "no", "是", "否", "嗯", "对", "行", "可以", "好", "谢谢",
742 "thanks", "hi", "hello", "你好", "开始", "start",
743 ];
744 generic.iter().any(|g| msg.eq_ignore_ascii_case(g))
745}