Skip to main content

do_memory_storage_turso/lib_impls/
config.rs

1//! Turso Configuration
2//!
3//! This module contains the TursoConfig struct and its implementation.
4
5use std::path::PathBuf;
6
7/// Storage mode for Turso storage
8#[derive(Debug, Clone)]
9pub enum StorageMode {
10    /// Local file-based SQLite via libsql
11    Local {
12        /// Path to the SQLite database file
13        path: PathBuf,
14    },
15    /// In-memory SQLite (tests, ephemeral agents)
16    InMemory,
17    /// Remote Turso Cloud / libSQL server
18    Remote {
19        /// Database URL
20        url: String,
21        /// Authentication token
22        auth_token: String,
23    },
24}
25
26impl Default for StorageMode {
27    fn default() -> Self {
28        // Default to local file-based SQLite using the workspace's default
29        // database path. The previous default of `Remote { url: "", ... }`
30        // silently produced an empty URL that failed at runtime; choosing
31        // `Local` keeps `Default::default()` safe to use as a placeholder
32        // and matches the new first-class local mode path.
33        StorageMode::Local {
34            path: do_memory_core::memory::default_db_path(),
35        }
36    }
37}
38
39/// Configuration for Turso storage
40#[derive(Debug, Clone)]
41pub struct TursoConfig {
42    /// Maximum retry attempts for failed operations
43    pub max_retries: u32,
44    /// Base delay for exponential backoff (milliseconds)
45    pub retry_base_delay_ms: u64,
46    /// Maximum delay for exponential backoff (milliseconds)
47    pub retry_max_delay_ms: u64,
48    /// Enable connection pooling
49    pub enable_pooling: bool,
50    /// Enable keep-alive connection pool (reduces connection overhead)
51    #[cfg(feature = "keepalive-pool")]
52    pub enable_keepalive: bool,
53    /// Keep-alive interval (seconds)
54    #[cfg(feature = "keepalive-pool")]
55    pub keepalive_interval_secs: u64,
56    /// Stale connection threshold (seconds)
57    #[cfg(feature = "keepalive-pool")]
58    pub stale_threshold_secs: u64,
59    /// Compression threshold in bytes (default: 1024 = 1KB)
60    /// Payloads smaller than this won't be compressed
61    /// Only used when compression feature is enabled
62    pub compression_threshold: usize,
63    /// Enable compression for episodes (default: true)
64    /// Only used when compression feature is enabled
65    pub compress_episodes: bool,
66    /// Enable compression for patterns (default: true)
67    /// Only used when compression feature is enabled
68    pub compress_patterns: bool,
69    /// Enable compression for embeddings (default: true)
70    /// Only used when compression feature is enabled
71    pub compress_embeddings: bool,
72    /// Compression level for zstd (1-22, default: 3)
73    /// 1 = fastest, 22 = best compression
74    /// Only used when compression feature is enabled
75    pub compression_level: i32,
76    /// Enable transport compression for network operations (default: true)
77    ///
78    /// **Architectural Note**: This flag is reserved for future custom transport implementations.
79    /// It is NOT currently wired into TursoStorage because libsql handles its own transport layer
80    /// internally. Compression IS applied at the data layer (embedding storage) via the
81    /// `compress()`/`decompress()` functions.
82    ///
83    /// For details, see `tests/runtime_wiring_transport_compression.rs` which documents:
84    /// - CompressedTransport is a standalone utility for custom Transport implementations
85    /// - TursoStorage uses libsql::Database directly, not the Transport trait
86    /// - Data-layer compression (at embedding level) is the correct architectural approach
87    #[cfg(feature = "compression")]
88    pub enable_transport_compression: bool,
89    /// Cache configuration for performance optimization
90    /// When None, caching is disabled (default: Some(CacheConfig::default()))
91    pub cache_config: Option<crate::cache::CacheConfig>,
92}
93
94impl Default for TursoConfig {
95    fn default() -> Self {
96        Self {
97            max_retries: 3,
98            retry_base_delay_ms: 100,
99            retry_max_delay_ms: 5000,
100            enable_pooling: true,
101            #[cfg(feature = "keepalive-pool")]
102            enable_keepalive: true,
103            #[cfg(feature = "keepalive-pool")]
104            keepalive_interval_secs: 30,
105            #[cfg(feature = "keepalive-pool")]
106            stale_threshold_secs: 60,
107            // Compression settings (always present, only used when compression feature is enabled)
108            compression_threshold: 1024,
109            compress_episodes: true,
110            compress_patterns: true,
111            compress_embeddings: true,
112            compression_level: 3, // Default zstd level (good balance of speed/ratio)
113            #[cfg(feature = "compression")]
114            enable_transport_compression: true,
115            // Cache configuration (enabled by default)
116            cache_config: Some(crate::cache::CacheConfig::default()),
117        }
118    }
119}