matrixcode-core 0.3.7

MatrixCode Agent Core - Pure logic, no UI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
use anyhow::Result;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use uuid::Uuid;

use crate::compress::CompressionHistoryEntry;
use crate::providers::Message;

/// Session metadata stored in the index.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionMetadata {
    /// Unique session identifier (UUID).
    pub id: String,
    /// User-defined session name (optional).
    pub name: Option<String>,
    /// Project path this session is associated with (optional).
    pub project_path: Option<String>,
    /// When the session was created.
    pub created_at: DateTime<Utc>,
    /// When the session was last updated.
    pub updated_at: DateTime<Utc>,
    /// Number of messages in the session.
    pub message_count: usize,
    /// Last input tokens reported.
    pub last_input_tokens: u64,
    /// Cumulative output tokens.
    pub total_output_tokens: u64,
    /// Compression history entries.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub compression_history: Vec<CompressionHistoryEntry>,
}

impl SessionMetadata {
    /// Create a new session metadata with a fresh UUID and auto-generated name.
    pub fn new(project_path: Option<&Path>) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4().to_string(),
            name: None,  // Will be auto-generated from first meaningful user message
            project_path: project_path.map(|p| p.to_string_lossy().to_string()),
            created_at: now,
            updated_at: now,
            message_count: 0,
            last_input_tokens: 0,
            total_output_tokens: 0,
            compression_history: Vec::new(),
        }
    }

    /// Generate a friendly time-based name for the session.
    /// Format: "YYYY-MM-DD HH:mm" (e.g., "2024-01-15 14:30")
    fn generate_time_name(time: DateTime<Utc>) -> String {
        // Use local timezone for display
        let local: chrono::DateTime<chrono::Local> = time.with_timezone(&chrono::Local);
        local.format("%Y-%m-%d %H:%M").to_string()
    }

    /// Add a compression entry to history.
    pub fn add_compression_entry(&mut self, entry: CompressionHistoryEntry) {
        self.compression_history.push(entry);
        // Keep only last 10 entries to avoid bloat
        if self.compression_history.len() > 10 {
            self.compression_history.remove(0);
        }
    }

    /// Get total tokens saved across all compressions.
    pub fn total_tokens_saved(&self) -> u32 {
        self.compression_history.iter().map(|e| e.tokens_saved).sum()
    }

    /// Get compression count.
    pub fn compression_count(&self) -> usize {
        self.compression_history.len()
    }

    /// Get a display name for the session.
    /// Returns user-defined name if set, otherwise a time-based fallback.
    pub fn display_name(&self) -> String {
        if let Some(ref name) = self.name {
            name.clone()
        } else {
            // Fallback: show creation time
            Self::generate_time_name(self.created_at)
        }
    }

    /// Get a short ID for the session (first 8 chars of UUID).
    pub fn short_id(&self) -> String {
        self.id[..8].to_string()
    }

    /// Format the session for display in a list.
    pub fn format_line(&self, is_current: bool) -> String {
        let marker = if is_current { "*" } else { " " };
        let name = self.display_name();
        let msgs = self.message_count;
        let project = self.project_path
            .as_ref()
            .map(|p| {
                // Show just the directory name, not full path
                PathBuf::from(p)
                    .file_name()
                    .map(|n| n.to_string_lossy().to_string())
                    .unwrap_or_else(|| p.clone())
            })
            .unwrap_or_else(|| "-".to_string());
        
        // Add compression info if any
        let compression_info = if self.compression_count() > 0 {
            format!("  💾 {} comps", self.compression_count())
        } else {
            "".to_string()
        };
        
        format!("{} {}  {} msgs  {}{}", marker, name, msgs, project, compression_info)
    }
}

/// Index of all sessions, stored in ~/.matrix/sessions/index.json
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SessionIndex {
    /// All known sessions.
    pub sessions: Vec<SessionMetadata>,
    /// ID of the most recently active session (for --continue).
    pub last_session_id: Option<String>,
}

impl SessionIndex {
    /// Find a session by ID or name.
    pub fn find(&self, query: &str) -> Option<&SessionMetadata> {
        // First try exact ID match
        if let Some(s) = self.sessions.iter().find(|s| s.id == query) {
            return Some(s);
        }
        // Then try exact name match
        if let Some(s) = self.sessions.iter().find(|s| s.name.as_deref() == Some(query)) {
            return Some(s);
        }
        // Then try partial ID match (for convenience)
        if let Some(s) = self.sessions.iter().find(|s| s.id.starts_with(query)) {
            return Some(s);
        }
        None
    }

    /// Get the last session (for --continue).
    pub fn last_session(&self) -> Option<&SessionMetadata> {
        self.last_session_id
            .as_ref()
            .and_then(|id| self.sessions.iter().find(|s| s.id == *id))
    }

    /// Add or update a session in the index.
    pub fn upsert(&mut self, meta: SessionMetadata) {
        // Remove existing entry with same ID
        self.sessions.retain(|s| s.id != meta.id);
        // Add new entry
        self.sessions.push(meta.clone());
        // Update last_session_id
        self.last_session_id = Some(meta.id);
        // Sort by updated_at descending (most recent first)
        self.sessions.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
    }

    /// Remove a session from the index.
    pub fn remove(&mut self, id: &str) -> Option<SessionMetadata> {
        let removed = self.sessions.iter().position(|s| s.id == id);
        if let Some(idx) = removed {
            let meta = self.sessions.remove(idx);
            if self.last_session_id.as_deref() == Some(id) {
                self.last_session_id = self.sessions.first().map(|s| s.id.clone());
            }
            Some(meta)
        } else {
            None
        }
    }

    /// Rename a session.
    pub fn rename(&mut self, id: &str, new_name: &str) -> Result<()> {
        let session = self.sessions.iter_mut().find(|s| s.id == id);
        if let Some(s) = session {
            s.name = Some(new_name.to_string());
            s.updated_at = Utc::now();
            Ok(())
        } else {
            anyhow::bail!("session {} not found", id)
        }
    }
}

/// Full session data including messages.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Session {
    pub metadata: SessionMetadata,
    pub messages: Vec<Message>,
}

impl Session {
    /// Create a new empty session.
    pub fn new(project_path: Option<&Path>) -> Self {
        Self {
            metadata: SessionMetadata::new(project_path),
            messages: Vec::new(),
        }
    }

    /// Create a session from existing messages.
    pub fn from_messages(messages: Vec<Message>, project_path: Option<&Path>) -> Self {
        let mut meta = SessionMetadata::new(project_path);
        meta.message_count = messages.len();
        Self {
            metadata: meta,
            messages,
        }
    }

    /// Update metadata after a turn.
    pub fn update_stats(&mut self, last_input_tokens: u32, total_output_tokens: u64) {
        self.metadata.message_count = self.messages.len();
        self.metadata.last_input_tokens = last_input_tokens as u64;
        self.metadata.total_output_tokens = total_output_tokens;
        self.metadata.updated_at = Utc::now();
    }
}

/// Manager for session storage.
pub struct SessionManager {
    /// Base directory for session storage (~/.matrix).
    base_dir: PathBuf,
    /// Current active session (if any).
    current_session: Option<Session>,
    /// Session index.
    index: SessionIndex,
}

impl SessionManager {
    /// Create a new session manager.
    pub fn new() -> Result<Self> {
        let base_dir = Self::get_base_dir()?;
        let manager = Self {
            base_dir,
            current_session: None,
            index: SessionIndex::default(),
        };
        manager.ensure_dirs()?;
        let mut manager = manager;
        manager.load_index()?;
        Ok(manager)
    }

    /// Get the base directory for session storage.
    fn get_base_dir() -> Result<PathBuf> {
        let home = std::env::var_os("HOME")
            .or_else(|| std::env::var_os("USERPROFILE"))
            .ok_or_else(|| anyhow::anyhow!("HOME or USERPROFILE environment variable not set"))?;
        let mut p = PathBuf::from(home);
        p.push(".matrix");
        Ok(p)
    }

    /// Get the sessions directory.
    fn sessions_dir(&self) -> PathBuf {
        self.base_dir.join("sessions")
    }

    /// Get the index file path.
    fn index_path(&self) -> PathBuf {
        self.sessions_dir().join("index.json")
    }

    /// Get the path for a specific session file.
    fn session_path(&self, id: &str) -> PathBuf {
        self.sessions_dir().join(format!("{}.json", id))
    }

    /// Ensure directories exist.
    fn ensure_dirs(&self) -> Result<()> {
        std::fs::create_dir_all(&self.base_dir)
            .with_context(|| format!("creating base dir {}", self.base_dir.display()))?;
        std::fs::create_dir_all(self.sessions_dir())
            .with_context(|| format!("creating sessions dir {}", self.sessions_dir().display()))?;
        Ok(())
    }

    /// Load the session index from disk.
    fn load_index(&mut self) -> Result<()> {
        let path = self.index_path();
        if !path.exists() {
            return Ok(());
        }
        let data = std::fs::read_to_string(&path)
            .with_context(|| format!("reading index file {}", path.display()))?;
        if data.trim().is_empty() {
            return Ok(());
        }
        self.index = serde_json::from_str(&data)
            .with_context(|| format!("parsing index file {}", path.display()))?;
        Ok(())
    }

    /// Save the session index to disk.
    fn save_index(&self) -> Result<()> {
        let path = self.index_path();
        let json = serde_json::to_string_pretty(&self.index)
            .context("serializing session index")?;
        let tmp = path.with_extension("json.tmp");
        std::fs::write(&tmp, json)
            .with_context(|| format!("writing index tmp file {}", tmp.display()))?;
        std::fs::rename(&tmp, &path)
            .with_context(|| format!("renaming index tmp file to {}", path.display()))?;
        Ok(())
    }

    /// Start a new session.
    pub fn start_new(&mut self, project_path: Option<&Path>) -> Result<&Session> {
        let session = Session::new(project_path);
        self.current_session = Some(session);
        self.save_current()?;
        Ok(self.current_session.as_ref().unwrap())
    }

    /// Continue the last session (for --continue).
    pub fn continue_last(&mut self, project_path: Option<&Path>) -> Result<Option<&Session>> {
        let last_id = self.index.last_session().map(|m| m.id.clone());
        if let Some(id) = last_id {
            self.load_session(&id)?;
            // Update project path if provided and different
            if let Some(path) = project_path
                && let Some(ref mut session) = self.current_session {
                    session.metadata.project_path = Some(path.to_string_lossy().to_string());
                }
            Ok(self.current_session.as_ref())
        } else {
            Ok(None)
        }
    }

    /// Resume a specific session by ID or name (for --resume).
    pub fn resume(&mut self, query: &str, project_path: Option<&Path>) -> Result<Option<&Session>> {
        let session_id = self.index.find(query).map(|m| m.id.clone());
        if let Some(id) = session_id {
            self.load_session(&id)?;
            // Update project path if provided
            if let Some(path) = project_path
                && let Some(ref mut session) = self.current_session {
                    session.metadata.project_path = Some(path.to_string_lossy().to_string());
                }
            Ok(self.current_session.as_ref())
        } else {
            Ok(None)
        }
    }

    /// Load a session from disk by ID.
    fn load_session(&mut self, id: &str) -> Result<()> {
        let path = self.session_path(id);
        if !path.exists() {
            anyhow::bail!("session file {} not found", path.display());
        }
        let data = std::fs::read_to_string(&path)
            .with_context(|| format!("reading session file {}", path.display()))?;
        let mut session: Session = serde_json::from_str(&data)
            .with_context(|| format!("parsing session file {}", path.display()))?;
        
        // If session name is null but index has a name, use index's name
        if session.metadata.name.is_none()
            && let Some(index_meta) = self.index.find(id) {
                session.metadata.name = index_meta.name.clone();
            }
        
        self.current_session = Some(session);
        Ok(())
    }

    /// Save the current session to disk.
    pub fn save_current(&mut self) -> Result<()> {
        if let Some(ref session) = self.current_session {
            let path = self.session_path(&session.metadata.id);
            let json = serde_json::to_string(session)
                .context("serializing session")?;
            let tmp = path.with_extension("json.tmp");
            std::fs::write(&tmp, json)
                .with_context(|| format!("writing session tmp file {}", tmp.display()))?;
            std::fs::rename(&tmp, &path)
                .with_context(|| format!("renaming session tmp file to {}", path.display()))?;
            
            // Update index
            self.index.upsert(session.metadata.clone());
            self.save_index()?;
        }
        Ok(())
    }

    /// Update current session stats after a turn.
    pub fn update_stats(&mut self, last_input_tokens: u32, total_output_tokens: u64) {
        if let Some(ref mut session) = self.current_session {
            session.update_stats(last_input_tokens, total_output_tokens);
        }
    }

    /// Record a compression event in the session history.
    pub fn record_compression(&mut self, entry: crate::compress::CompressionHistoryEntry) {
        if let Some(ref mut session) = self.current_session {
            session.metadata.add_compression_entry(entry);
        }
    }

    /// Set messages for the current session.
    pub fn set_messages(&mut self, messages: Vec<Message>) {
        if let Some(ref mut session) = self.current_session {
            // Auto-generate name from first user message if name is None
            if session.metadata.name.is_none() && !messages.is_empty()
                && let Some(name) = Self::generate_name_from_messages(&messages) {
                    session.metadata.name = Some(name);
                }
            
            session.messages = messages;
            session.metadata.message_count = session.messages.len();
            session.metadata.updated_at = Utc::now();
        }
    }

    /// Generate a human-readable session name from the first user message.
    /// Takes the first meaningful user input and truncates it.
    fn generate_name_from_messages(messages: &[Message]) -> Option<String> {
        use crate::providers::{Role, MessageContent, ContentBlock};
        
        // Find first meaningful user message (skip very short/generic ones)
        let user_messages: Vec<&Message> = messages.iter()
            .filter(|m| m.role == Role::User)
            .collect();
        
        for msg in user_messages.iter().take(3) {
            let text = match &msg.content {
                MessageContent::Text(t) => t.clone(),
                MessageContent::Blocks(blocks) => {
                    blocks.iter().filter_map(|b| {
                        if let ContentBlock::Text { text } = b {
                            Some(text.clone())
                        } else {
                            None
                        }
                    }).collect::<Vec<_>>().join(" ")
                }
            };
            
            let cleaned = text.trim().lines().next().unwrap_or("").trim();
            
            // Skip too short or generic messages
            if cleaned.len() < 5 || is_generic_message(cleaned) {
                continue;
            }
            
            // Truncate to reasonable length for display
            let name = if cleaned.chars().count() > 40 {
                let truncated: String = cleaned.chars().take(37).collect();
                format!("{}...", truncated)
            } else {
                cleaned.to_string()
            };
            
            return Some(name);
        }
        
        None
    }

    /// Get the current session's messages.
    pub fn messages(&self) -> Option<&[Message]> {
        self.current_session.as_ref().map(|s| s.messages.as_slice())
    }

    /// Get mutable reference to messages.
    pub fn messages_mut(&mut self) -> Option<&mut Vec<Message>> {
        self.current_session.as_mut().map(|s| &mut s.messages)
    }

    /// Get the current session ID.
    pub fn current_id(&self) -> Option<&str> {
        self.current_session.as_ref().map(|s| s.metadata.id.as_str())
    }

    /// Get the current session name.
    pub fn current_name(&self) -> Option<&str> {
        self.current_session.as_ref().and_then(|s| s.name())
    }

    /// Rename the current session.
    pub fn rename_current(&mut self, new_name: &str) -> Result<()> {
        if let Some(ref session) = self.current_session {
            let id = session.metadata.id.clone();
            self.index.rename(&id, new_name)?;
            if let Some(ref mut session) = self.current_session {
                session.metadata.name = Some(new_name.to_string());
            }
            self.save_current()?;
        }
        Ok(())
    }

    /// Clear the current session (start fresh).
    pub fn clear_current(&mut self) -> Result<()> {
        if let Some(ref session) = self.current_session {
            // Remove session file
            let path = self.session_path(&session.metadata.id);
            let _ = std::fs::remove_file(&path);
            // Remove from index
            self.index.remove(&session.metadata.id);
            self.save_index()?;
        }
        self.current_session = None;
        Ok(())
    }

    /// List all sessions.
    pub fn list_sessions(&self) -> &[SessionMetadata] {
        &self.index.sessions
    }

    /// Check if there's a current session.
    pub fn has_current(&self) -> bool {
        self.current_session.is_some()
    }

    /// Get current session metadata.
    pub fn current_metadata(&self) -> Option<&SessionMetadata> {
        self.current_session.as_ref().map(|s| &s.metadata)
    }

    /// Get the history file path (legacy compatibility).
    pub fn history_path(&self) -> PathBuf {
        self.base_dir.join("history.txt")
    }
}

impl Session {
    /// Get the session name (user-defined or fallback).
    pub fn name(&self) -> Option<&str> {
        self.metadata.name.as_deref()
    }
}

use anyhow::Context;

/// Check if a message is too generic to be a good session name.
fn is_generic_message(msg: &str) -> bool {
    let generic = [
        "继续", "好的", "ok", "yes", "no", "", "",
        "", "", "", "可以", "", "谢谢", "thanks",
        "hi", "hello", "你好", "开始", "start",
    ];
    generic.iter().any(|g| msg.eq_ignore_ascii_case(g))
}