zeptoclaw 0.7.2

Ultra-lightweight personal AI assistant
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! CLI conversation history management for ZeptoClaw.
//!
//! This module provides discovery, listing, and cleanup of CLI conversation
//! sessions stored on disk. It operates as a read-only layer on top of the
//! existing `SessionManager` persistence format, filtering only sessions
//! whose key starts with `"cli:"`.

use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

use serde::{Deserialize, Serialize};

use crate::config::Config;
use crate::error::{Result, ZeptoError};
use crate::session::{Message, Role, Session};

/// Metadata for a saved CLI conversation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationEntry {
    /// Unique session key (e.g., "cli:1739523000")
    pub session_key: String,
    /// Human-readable title (first user message, truncated to 80 chars)
    pub title: String,
    /// Number of messages in the conversation
    pub message_count: usize,
    /// When the conversation was last updated (ISO 8601)
    pub last_updated: String,
    /// File size in bytes
    pub file_size: u64,
}

/// Manages CLI conversation history on disk.
///
/// Scans the session storage directory for files that correspond to CLI
/// sessions (keys starting with `"cli:"`), reads their metadata, and
/// provides listing, search, and cleanup operations.
pub struct ConversationHistory {
    storage_path: PathBuf,
}

impl ConversationHistory {
    /// Create a new `ConversationHistory` using the default sessions directory.
    ///
    /// The default path is `~/.zeptoclaw/sessions/`.
    ///
    /// # Errors
    ///
    /// Returns an error if the sessions directory cannot be created.
    pub fn new() -> Result<Self> {
        let storage_path = Config::dir().join("sessions");
        std::fs::create_dir_all(&storage_path)?;
        Ok(Self { storage_path })
    }

    /// Create a new `ConversationHistory` with a custom storage path.
    ///
    /// Useful for testing with temporary directories.
    ///
    /// # Errors
    ///
    /// Returns an error if the directory cannot be created.
    pub fn with_path(path: PathBuf) -> Result<Self> {
        std::fs::create_dir_all(&path)?;
        Ok(Self { storage_path: path })
    }

    /// List all CLI conversations, sorted by `last_updated` descending (newest first).
    ///
    /// Scans the session directory for `cli%3A*.json` files, reads each one to
    /// extract metadata, and returns a sorted list of conversation entries.
    ///
    /// Non-CLI sessions (e.g., telegram, slack) are ignored.
    ///
    /// # Errors
    ///
    /// Returns an error if reading the directory or any session file fails.
    pub fn list_conversations(&self) -> Result<Vec<ConversationEntry>> {
        let mut entries = Vec::new();

        let dir_entries = std::fs::read_dir(&self.storage_path)?;
        for entry in dir_entries {
            let entry = entry?;
            let path = entry.path();

            // Only look at .json files whose sanitized name starts with cli%3A
            let file_name = match path.file_name().and_then(|n| n.to_str()) {
                Some(name) => name.to_string(),
                None => continue,
            };

            if !file_name.ends_with(".json") || !file_name.starts_with("cli%3A") {
                continue;
            }

            // Read and parse the session
            let content = match std::fs::read_to_string(&path) {
                Ok(c) => c,
                Err(_) => continue,
            };

            let session: Session = match serde_json::from_str(&content) {
                Ok(s) => s,
                Err(_) => continue,
            };

            // Only include sessions with CLI keys
            if !session.key.starts_with("cli:") {
                continue;
            }

            let file_size = entry.metadata().map(|m| m.len()).unwrap_or(0);

            entries.push(ConversationEntry {
                session_key: session.key.clone(),
                title: Self::extract_title(&session.messages),
                message_count: session.messages.len(),
                last_updated: session.updated_at.to_rfc3339(),
                file_size,
            });
        }

        // Sort by last_updated descending (newest first)
        entries.sort_by(|a, b| b.last_updated.cmp(&a.last_updated));

        Ok(entries)
    }

    /// Return the most recently updated CLI conversation, if any.
    ///
    /// # Errors
    ///
    /// Returns an error if listing conversations fails.
    pub fn latest_conversation(&self) -> Result<Option<ConversationEntry>> {
        let conversations = self.list_conversations()?;
        Ok(conversations.into_iter().next())
    }

    /// Find a CLI conversation by title substring (case-insensitive) or exact session key.
    ///
    /// First attempts an exact match on session key, then falls back to a
    /// case-insensitive substring match on the title. Returns the first match.
    ///
    /// # Errors
    ///
    /// Returns an error if listing conversations fails.
    pub fn find_conversation(&self, query: &str) -> Result<Option<ConversationEntry>> {
        let conversations = self.list_conversations()?;

        // Try exact session key match first
        if let Some(entry) = conversations.iter().find(|e| e.session_key == query) {
            return Ok(Some(entry.clone()));
        }

        // Fall back to case-insensitive title substring match
        let query_lower = query.to_lowercase();
        Ok(conversations
            .into_iter()
            .find(|e| e.title.to_lowercase().contains(&query_lower)))
    }

    /// Generate a unique CLI session key using the current unix timestamp.
    ///
    /// Format: `cli:<unix_epoch_seconds>`
    pub fn generate_session_key() -> String {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        format!("cli:{}", timestamp)
    }

    /// Extract a title from a list of messages.
    ///
    /// Finds the first user message and truncates its content to 80 characters.
    /// Returns `"(empty conversation)"` if there are no messages, or
    /// `"(no user messages)"` if no user messages are found.
    pub fn extract_title(messages: &[Message]) -> String {
        if messages.is_empty() {
            return "(empty conversation)".to_string();
        }

        match messages.iter().find(|m| m.role == Role::User) {
            Some(msg) => {
                let content = msg.content.trim();
                if content.len() <= 80 {
                    content.to_string()
                } else {
                    let mut truncated: String = content.chars().take(80).collect();
                    truncated.push_str("...");
                    truncated
                }
            }
            None => "(no user messages)".to_string(),
        }
    }

    /// Delete the oldest CLI conversations, keeping only the most recent `keep_count`.
    ///
    /// Conversations are sorted by `last_updated` descending, so the newest
    /// `keep_count` are preserved and the rest are deleted from disk.
    ///
    /// # Returns
    ///
    /// The number of conversations deleted.
    ///
    /// # Errors
    ///
    /// Returns an error if listing or deleting session files fails.
    pub fn cleanup_old(&self, keep_count: usize) -> Result<usize> {
        let conversations = self.list_conversations()?;

        if conversations.len() <= keep_count {
            return Ok(0);
        }

        let to_delete = &conversations[keep_count..];
        let mut deleted = 0;

        for entry in to_delete {
            let sanitized = Self::sanitize_key(&entry.session_key);
            let file_path = self.storage_path.join(format!("{}.json", sanitized));
            if file_path.exists() {
                std::fs::remove_file(&file_path).map_err(|e| {
                    ZeptoError::Session(format!(
                        "Failed to delete session file {}: {}",
                        file_path.display(),
                        e
                    ))
                })?;
                deleted += 1;
            }
        }

        Ok(deleted)
    }

    /// Sanitize a session key for use as a filename (matches `SessionManager::sanitize_key`).
    fn sanitize_key(key: &str) -> String {
        let mut result = String::with_capacity(key.len() * 3);
        for c in key.chars() {
            match c {
                '/' => result.push_str("%2F"),
                '\\' => result.push_str("%5C"),
                ':' => result.push_str("%3A"),
                '*' => result.push_str("%2A"),
                '?' => result.push_str("%3F"),
                '"' => result.push_str("%22"),
                '<' => result.push_str("%3C"),
                '>' => result.push_str("%3E"),
                '|' => result.push_str("%7C"),
                '%' => result.push_str("%25"),
                c => result.push(c),
            }
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;
    use tempfile::TempDir;

    /// Helper to write a test session JSON file directly to disk.
    fn write_test_session(dir: &Path, key: &str, user_msg: &str, updated_at: &str) {
        // Build the session JSON manually to control the updated_at timestamp.
        // The real Session uses chrono DateTime, so we produce compatible JSON.
        let session_json = serde_json::json!({
            "key": key,
            "messages": [{
                "role": "user",
                "content": user_msg
            }],
            "summary": null,
            "created_at": updated_at,
            "updated_at": updated_at
        });
        let sanitized = key.replace(':', "%3A");
        let path = dir.join(format!("{}.json", sanitized));
        std::fs::write(path, serde_json::to_string(&session_json).unwrap()).unwrap();
    }

    #[test]
    fn test_conversation_entry_creation() {
        let entry = ConversationEntry {
            session_key: "cli:1700000000".to_string(),
            title: "Hello world".to_string(),
            message_count: 3,
            last_updated: "2025-11-14T22:13:20+00:00".to_string(),
            file_size: 256,
        };

        assert_eq!(entry.session_key, "cli:1700000000");
        assert_eq!(entry.title, "Hello world");
        assert_eq!(entry.message_count, 3);
        assert_eq!(entry.last_updated, "2025-11-14T22:13:20+00:00");
        assert_eq!(entry.file_size, 256);
    }

    #[test]
    fn test_generate_session_key() {
        let key = ConversationHistory::generate_session_key();
        assert!(
            key.starts_with("cli:"),
            "Key should start with 'cli:', got: {}",
            key
        );

        // The remainder should be parseable as a u64 timestamp
        let timestamp_part = &key[4..];
        let parsed: u64 = timestamp_part
            .parse()
            .expect("Timestamp part should be a valid u64");
        assert!(parsed > 0, "Timestamp should be positive");
    }

    #[test]
    fn test_extract_title_from_messages() {
        let messages = vec![
            Message::system("You are helpful"),
            Message::user("Tell me about Rust programming language and its memory safety features"),
        ];

        let title = ConversationHistory::extract_title(&messages);
        assert_eq!(
            title,
            "Tell me about Rust programming language and its memory safety features"
        );

        // Test truncation at 80 chars
        let long_msg = "a".repeat(120);
        let messages = vec![Message::user(&long_msg)];
        let title = ConversationHistory::extract_title(&messages);
        assert!(
            title.len() <= 83,
            "Title should be at most 80 chars + '...'"
        );
        assert!(title.ends_with("..."), "Long title should end with '...'");
        // The first 80 chars should be preserved
        let title_prefix: String = title.chars().take(80).collect();
        let long_msg_prefix: String = long_msg.chars().take(80).collect();
        assert_eq!(title_prefix, long_msg_prefix);
    }

    #[test]
    fn test_extract_title_empty_messages() {
        let messages: Vec<Message> = vec![];
        let title = ConversationHistory::extract_title(&messages);
        assert_eq!(title, "(empty conversation)");
    }

    #[test]
    fn test_extract_title_no_user_messages() {
        let messages = vec![
            Message::system("System prompt"),
            Message::assistant("Hello there"),
        ];
        let title = ConversationHistory::extract_title(&messages);
        assert_eq!(title, "(no user messages)");
    }

    #[test]
    fn test_list_conversations_empty_dir() {
        let temp_dir = TempDir::new().unwrap();
        let history = ConversationHistory::with_path(temp_dir.path().to_path_buf()).unwrap();

        let conversations = history.list_conversations().unwrap();
        assert!(conversations.is_empty());
    }

    #[test]
    fn test_list_conversations_with_sessions() {
        let temp_dir = TempDir::new().unwrap();
        let dir = temp_dir.path();

        write_test_session(
            dir,
            "cli:1000",
            "First conversation",
            "2025-01-01T00:00:00Z",
        );
        write_test_session(
            dir,
            "cli:2000",
            "Second conversation",
            "2025-06-15T12:00:00Z",
        );
        write_test_session(
            dir,
            "cli:3000",
            "Third conversation",
            "2025-03-10T06:30:00Z",
        );

        let history = ConversationHistory::with_path(dir.to_path_buf()).unwrap();
        let conversations = history.list_conversations().unwrap();

        assert_eq!(conversations.len(), 3);

        // Should be sorted by last_updated descending (newest first)
        assert_eq!(conversations[0].session_key, "cli:2000");
        assert_eq!(conversations[1].session_key, "cli:3000");
        assert_eq!(conversations[2].session_key, "cli:1000");

        // Verify titles
        assert_eq!(conversations[0].title, "Second conversation");
        assert_eq!(conversations[1].title, "Third conversation");
        assert_eq!(conversations[2].title, "First conversation");

        // Verify message counts
        for conv in &conversations {
            assert_eq!(conv.message_count, 1);
        }
    }

    #[test]
    fn test_list_conversations_ignores_non_cli() {
        let temp_dir = TempDir::new().unwrap();
        let dir = temp_dir.path();

        // Write a CLI session
        write_test_session(dir, "cli:1000", "CLI session", "2025-01-01T00:00:00Z");

        // Write a telegram session (should be ignored)
        write_test_session(
            dir,
            "telegram:chat123",
            "Telegram session",
            "2025-01-02T00:00:00Z",
        );

        // Write a slack session (should be ignored)
        write_test_session(
            dir,
            "slack:channel456",
            "Slack session",
            "2025-01-03T00:00:00Z",
        );

        let history = ConversationHistory::with_path(dir.to_path_buf()).unwrap();
        let conversations = history.list_conversations().unwrap();

        assert_eq!(conversations.len(), 1);
        assert_eq!(conversations[0].session_key, "cli:1000");
        assert_eq!(conversations[0].title, "CLI session");
    }

    #[test]
    fn test_latest_conversation() {
        let temp_dir = TempDir::new().unwrap();
        let dir = temp_dir.path();

        write_test_session(dir, "cli:1000", "Old conversation", "2025-01-01T00:00:00Z");
        write_test_session(
            dir,
            "cli:2000",
            "Newest conversation",
            "2025-12-31T23:59:59Z",
        );
        write_test_session(
            dir,
            "cli:3000",
            "Middle conversation",
            "2025-06-15T12:00:00Z",
        );

        let history = ConversationHistory::with_path(dir.to_path_buf()).unwrap();
        let latest = history.latest_conversation().unwrap();

        assert!(latest.is_some());
        let latest = latest.unwrap();
        assert_eq!(latest.session_key, "cli:2000");
        assert_eq!(latest.title, "Newest conversation");
    }

    #[test]
    fn test_latest_conversation_empty() {
        let temp_dir = TempDir::new().unwrap();
        let history = ConversationHistory::with_path(temp_dir.path().to_path_buf()).unwrap();

        let latest = history.latest_conversation().unwrap();
        assert!(latest.is_none());
    }

    #[test]
    fn test_find_conversation_by_title() {
        let temp_dir = TempDir::new().unwrap();
        let dir = temp_dir.path();

        write_test_session(
            dir,
            "cli:1000",
            "Discuss Rust memory safety",
            "2025-01-01T00:00:00Z",
        );
        write_test_session(
            dir,
            "cli:2000",
            "Python web framework comparison",
            "2025-06-15T12:00:00Z",
        );

        let history = ConversationHistory::with_path(dir.to_path_buf()).unwrap();

        // Case-insensitive substring match
        let found = history.find_conversation("rust memory").unwrap();
        assert!(found.is_some());
        assert_eq!(found.unwrap().session_key, "cli:1000");

        // Case-insensitive match with different casing
        let found = history.find_conversation("PYTHON WEB").unwrap();
        assert!(found.is_some());
        assert_eq!(found.unwrap().session_key, "cli:2000");

        // No match
        let found = history.find_conversation("nonexistent topic").unwrap();
        assert!(found.is_none());
    }

    #[test]
    fn test_find_conversation_by_key() {
        let temp_dir = TempDir::new().unwrap();
        let dir = temp_dir.path();

        write_test_session(dir, "cli:1000", "First session", "2025-01-01T00:00:00Z");
        write_test_session(dir, "cli:2000", "Second session", "2025-06-15T12:00:00Z");

        let history = ConversationHistory::with_path(dir.to_path_buf()).unwrap();

        // Exact session key match
        let found = history.find_conversation("cli:1000").unwrap();
        assert!(found.is_some());
        let entry = found.unwrap();
        assert_eq!(entry.session_key, "cli:1000");
        assert_eq!(entry.title, "First session");
    }

    #[test]
    fn test_cleanup_old() {
        let temp_dir = TempDir::new().unwrap();
        let dir = temp_dir.path();

        // Create 5 CLI sessions with distinct timestamps
        write_test_session(dir, "cli:1000", "Session one", "2025-01-01T00:00:00Z");
        write_test_session(dir, "cli:2000", "Session two", "2025-02-01T00:00:00Z");
        write_test_session(dir, "cli:3000", "Session three", "2025-03-01T00:00:00Z");
        write_test_session(dir, "cli:4000", "Session four", "2025-04-01T00:00:00Z");
        write_test_session(dir, "cli:5000", "Session five", "2025-05-01T00:00:00Z");

        let history = ConversationHistory::with_path(dir.to_path_buf()).unwrap();

        // Verify we have 5 conversations
        assert_eq!(history.list_conversations().unwrap().len(), 5);

        // Cleanup, keeping only the 2 newest
        let deleted = history.cleanup_old(2).unwrap();
        assert_eq!(deleted, 3);

        // Verify only 2 remain (the newest ones)
        let remaining = history.list_conversations().unwrap();
        assert_eq!(remaining.len(), 2);
        assert_eq!(remaining[0].session_key, "cli:5000");
        assert_eq!(remaining[1].session_key, "cli:4000");

        // The older 3 files should be gone
        assert!(!dir.join("cli%3A1000.json").exists());
        assert!(!dir.join("cli%3A2000.json").exists());
        assert!(!dir.join("cli%3A3000.json").exists());

        // The newer 2 files should still exist
        assert!(dir.join("cli%3A4000.json").exists());
        assert!(dir.join("cli%3A5000.json").exists());
    }

    #[test]
    fn test_cleanup_old_nothing_to_delete() {
        let temp_dir = TempDir::new().unwrap();
        let dir = temp_dir.path();

        write_test_session(dir, "cli:1000", "Session one", "2025-01-01T00:00:00Z");
        write_test_session(dir, "cli:2000", "Session two", "2025-02-01T00:00:00Z");

        let history = ConversationHistory::with_path(dir.to_path_buf()).unwrap();

        // keep_count >= total conversations, nothing deleted
        let deleted = history.cleanup_old(5).unwrap();
        assert_eq!(deleted, 0);
        assert_eq!(history.list_conversations().unwrap().len(), 2);
    }
}