lore_cli/storage/mod.rs
1//! Storage layer for Lore.
2//!
3//! This module provides SQLite-based persistence for sessions, messages,
4//! and session-to-commit links. It handles schema migrations and provides
5//! query methods for the CLI and future daemon.
6//!
7//! # Submodules
8//!
9//! - `db` - Database connection and query operations
10//! - `models` - Data structures for sessions, messages, and links
11
12use crate::config::Config;
13
14/// SQLite database connection and query operations.
15pub mod db;
16
17/// Data structures representing sessions, messages, and links.
18pub mod models;
19
20pub use db::Database;
21// DatabaseStats is also available at crate::storage::db::DatabaseStats if needed
22pub use models::{
23 extract_session_files, Annotation, ContentBlock, LinkCreator, LinkType, Machine,
24 MessageContent, MessageRole, SessionLink, Summary, Tag,
25};
26
27// Re-exported for use by integration tests. These types are used through the
28// storage module in tests/cli_integration.rs even though they're not directly
29// used in the binary crate itself.
30#[allow(unused_imports)]
31pub use models::{Message, Session};
32
33/// Returns the machine UUID for the current machine.
34///
35/// Loads the config and returns the machine_id (UUID), generating one if needed.
36/// Used to populate the `machine_id` field on sessions, allowing cloud sync
37/// to identify which machine created a session. Returns `None` if the config
38/// cannot be loaded or the machine ID cannot be determined.
39pub fn get_machine_id() -> Option<String> {
40 Config::load()
41 .ok()
42 .and_then(|mut config| config.get_or_create_machine_id().ok())
43}
44
45/// Returns a display-friendly name for a machine ID.
46///
47/// First queries the machines table to find a registered name. If not found,
48/// falls back to checking if this is the current machine and uses the config.
49/// Otherwise returns the machine_id truncated to first 8 characters for readability.
50///
51/// This function is designed for use in session listings to show human-readable
52/// machine names instead of UUIDs.
53#[allow(dead_code)]
54pub fn get_machine_display_name(db: &Database, machine_id: &str) -> String {
55 // First try to get from the machines table
56 if let Ok(name) = db.get_machine_name(machine_id) {
57 // get_machine_name returns the name if found, or truncated ID if not
58 // Check if we got a full name (not just truncated ID)
59 if let Ok(Some(_machine)) = db.get_machine(machine_id) {
60 return name;
61 }
62 }
63
64 // Fall back to checking if this is the current machine
65 if let Ok(mut config) = Config::load() {
66 if let Ok(current_id) = config.get_or_create_machine_id() {
67 if machine_id == current_id {
68 return config.get_machine_name();
69 }
70 }
71 }
72
73 // Not found anywhere, show truncated UUID
74 if machine_id.len() >= 8 {
75 format!("{}...", &machine_id[..8])
76 } else {
77 machine_id.to_string()
78 }
79}