ulm 0.3.2

AI-powered manpage assistant using local LLM
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
//! Configuration file management.
//!
//! This module handles loading and saving application configuration
//! using XDG-compliant paths and TOML format.

use std::fs;
use std::path::PathBuf;

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

/// Application configuration.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Config {
    /// Model configuration.
    pub models: ModelsConfig,
    /// Ollama server configuration.
    pub ollama: OllamaConfig,
    /// Index metadata.
    pub index: IndexConfig,
}

/// Model configuration for embedding and LLM.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ModelsConfig {
    /// Model used for generating embeddings (index + query).
    pub embedding_model: String,
    /// Model used for LLM response generation.
    pub llm_model: String,
}

/// Ollama server configuration.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OllamaConfig {
    /// The URL of the Ollama server.
    pub url: String,
    /// Timeout for LLM generation requests in seconds.
    #[serde(default = "default_generate_timeout")]
    pub generate_timeout_secs: u64,
    /// Timeout for embedding requests in seconds.
    #[serde(default = "default_embedding_timeout")]
    pub embedding_timeout_secs: u64,
}

/// Default timeout for LLM generation (120 seconds).
const fn default_generate_timeout() -> u64 {
    120
}

/// Default timeout for embedding requests (60 seconds).
const fn default_embedding_timeout() -> u64 {
    60
}

/// Index metadata for validation.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IndexConfig {
    /// Embedding dimension used when building index.
    pub embedding_dimension: Option<u32>,
    /// Model name used when building index.
    pub last_embedding_model: Option<String>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            models: ModelsConfig {
                embedding_model: "nomic-embed-text".to_string(),
                llm_model: "llama3.2:3b".to_string(),
            },
            ollama: OllamaConfig {
                url: "http://localhost:11434".to_string(),
                generate_timeout_secs: default_generate_timeout(),
                embedding_timeout_secs: default_embedding_timeout(),
            },
            index: IndexConfig {
                embedding_dimension: None,
                last_embedding_model: None,
            },
        }
    }
}

impl Config {
    /// Get the embedding model name.
    #[must_use]
    pub fn embedding_model(&self) -> &str {
        &self.models.embedding_model
    }

    /// Get the LLM model name.
    #[must_use]
    pub fn llm_model(&self) -> &str {
        &self.models.llm_model
    }

    /// Get the Ollama URL.
    #[must_use]
    pub fn ollama_url(&self) -> &str {
        &self.ollama.url
    }

    /// Get the generate timeout in seconds.
    #[must_use]
    pub const fn generate_timeout_secs(&self) -> u64 {
        self.ollama.generate_timeout_secs
    }

    /// Get the embedding timeout in seconds.
    #[must_use]
    pub const fn embedding_timeout_secs(&self) -> u64 {
        self.ollama.embedding_timeout_secs
    }

    /// Update index metadata after building index.
    pub fn update_index_metadata(&mut self, dimension: u32) {
        self.index.embedding_dimension = Some(dimension);
        self.index.last_embedding_model = Some(self.models.embedding_model.clone());
    }

    /// Check if index needs rebuild due to model change.
    #[must_use]
    pub fn needs_index_rebuild(&self) -> bool {
        match &self.index.last_embedding_model {
            Some(last_model) => last_model != &self.models.embedding_model,
            None => false, // No previous index, not a "rebuild"
        }
    }

    /// Get index dimension if available.
    #[must_use]
    pub const fn index_dimension(&self) -> Option<u32> {
        self.index.embedding_dimension
    }
}

/// Legacy configuration format for migration.
#[derive(Debug, Deserialize)]
#[allow(clippy::missing_docs_in_private_items)]
struct LegacyConfig {
    model_name: String,
    ollama_url: String,
}

/// Gets the XDG-compliant config file path.
///
/// Returns `~/.config/ulm/config.toml` on Linux/macOS.
///
/// # Errors
///
/// Returns an error if the config directory cannot be determined.
pub fn get_config_path() -> Result<PathBuf> {
    let dirs = directories::ProjectDirs::from("", "", "ulm")
        .context("Could not determine config directory")?;

    let config_dir = dirs.config_dir();

    // Create config directory if needed
    fs::create_dir_all(config_dir).with_context(|| {
        format!(
            "Failed to create config directory: {}",
            config_dir.display()
        )
    })?;

    Ok(config_dir.join("config.toml"))
}

/// Loads configuration from file with automatic migration.
///
/// Returns default configuration if the file doesn't exist.
/// Automatically migrates legacy single-model format to new multi-model format.
///
/// # Errors
///
/// Returns an error if the file exists but cannot be read or parsed.
pub fn load_config() -> Result<Config> {
    let config_path = get_config_path()?;

    if !config_path.exists() {
        return Ok(Config::default());
    }

    let content = fs::read_to_string(&config_path)
        .with_context(|| format!("Failed to read config file: {}", config_path.display()))?;

    // Try to parse as new format first
    if let Ok(config) = toml::from_str::<Config>(&content) {
        return Ok(config);
    }

    // Try to parse as legacy format and migrate
    if let Ok(legacy) = toml::from_str::<LegacyConfig>(&content) {
        tracing::info!("Migrating legacy config to new multi-model format");

        let config = Config {
            models: ModelsConfig {
                // Use legacy model for both (user can change later)
                embedding_model: legacy.model_name.clone(),
                llm_model: legacy.model_name,
            },
            ollama: OllamaConfig {
                url: legacy.ollama_url,
                generate_timeout_secs: default_generate_timeout(),
                embedding_timeout_secs: default_embedding_timeout(),
            },
            index: IndexConfig {
                embedding_dimension: None,
                last_embedding_model: None,
            },
        };

        // Save migrated config
        save_config(&config)?;
        tracing::info!("Config migrated successfully");

        return Ok(config);
    }

    // Neither format worked
    anyhow::bail!("Failed to parse config file: {}", config_path.display())
}

/// Saves configuration to file.
///
/// Creates the config directory if it doesn't exist.
/// Sets file permissions to 0600 (user-only) on Unix systems.
///
/// # Errors
///
/// Returns an error if the file cannot be written.
pub fn save_config(config: &Config) -> Result<()> {
    let config_path = get_config_path()?;

    let content = toml::to_string_pretty(config).context("Failed to serialize config")?;

    fs::write(&config_path, &content)
        .with_context(|| format!("Failed to write config file: {}", config_path.display()))?;

    // Set file permissions to user-only on Unix
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let permissions = fs::Permissions::from_mode(0o600);
        fs::set_permissions(&config_path, permissions)
            .with_context(|| format!("Failed to set permissions on: {}", config_path.display()))?;
    }

    Ok(())
}

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

    #[test]
    fn test_config_default() {
        let config = Config::default();
        assert_eq!(config.models.embedding_model, "nomic-embed-text");
        assert_eq!(config.models.llm_model, "llama3.2:3b");
        assert_eq!(config.ollama.url, "http://localhost:11434");
        assert_eq!(config.ollama.generate_timeout_secs, 120);
        assert_eq!(config.ollama.embedding_timeout_secs, 60);
        assert_eq!(config.index.embedding_dimension, None);
    }

    #[test]
    fn test_config_serialization() {
        let config = Config {
            models: ModelsConfig {
                embedding_model: "nomic-embed-text".to_string(),
                llm_model: "mistral:7b".to_string(),
            },
            ollama: OllamaConfig {
                url: "http://localhost:11434".to_string(),
                generate_timeout_secs: 180,
                embedding_timeout_secs: 90,
            },
            index: IndexConfig {
                embedding_dimension: Some(768),
                last_embedding_model: Some("nomic-embed-text".to_string()),
            },
        };

        let toml_str = toml::to_string(&config).unwrap();
        assert!(toml_str.contains("embedding_model = \"nomic-embed-text\""));
        assert!(toml_str.contains("llm_model = \"mistral:7b\""));
        assert!(toml_str.contains("embedding_dimension = 768"));
        assert!(toml_str.contains("generate_timeout_secs = 180"));
        assert!(toml_str.contains("embedding_timeout_secs = 90"));
    }

    #[test]
    fn test_config_deserialization() {
        let toml_str = r#"
            [models]
            embedding_model = "mxbai-embed-large"
            llm_model = "llama3.1:8b"

            [ollama]
            url = "http://localhost:11434"
            generate_timeout_secs = 300
            embedding_timeout_secs = 120

            [index]
            embedding_dimension = 1024
            last_embedding_model = "mxbai-embed-large"
        "#;

        let config: Config = toml::from_str(toml_str).unwrap();
        assert_eq!(config.models.embedding_model, "mxbai-embed-large");
        assert_eq!(config.models.llm_model, "llama3.1:8b");
        assert_eq!(config.index.embedding_dimension, Some(1024));
        assert_eq!(config.ollama.generate_timeout_secs, 300);
        assert_eq!(config.ollama.embedding_timeout_secs, 120);
    }

    #[test]
    fn test_config_deserialization_with_defaults() {
        // Test that old configs without timeout fields still work
        let toml_str = r#"
            [models]
            embedding_model = "nomic-embed-text"
            llm_model = "llama3"

            [ollama]
            url = "http://localhost:11434"

            [index]
        "#;

        let config: Config = toml::from_str(toml_str).unwrap();
        assert_eq!(config.ollama.generate_timeout_secs, 120);
        assert_eq!(config.ollama.embedding_timeout_secs, 60);
    }

    #[test]
    fn test_legacy_config_migration() {
        let legacy_toml = r#"
            model_name = "llama3.1:8b"
            ollama_url = "http://localhost:11434"
        "#;

        // Parse as legacy
        let legacy: LegacyConfig = toml::from_str(legacy_toml).unwrap();

        // Migrate
        let config = Config {
            models: ModelsConfig {
                embedding_model: legacy.model_name.clone(),
                llm_model: legacy.model_name,
            },
            ollama: OllamaConfig {
                url: legacy.ollama_url,
                generate_timeout_secs: default_generate_timeout(),
                embedding_timeout_secs: default_embedding_timeout(),
            },
            index: IndexConfig {
                embedding_dimension: None,
                last_embedding_model: None,
            },
        };

        assert_eq!(config.models.embedding_model, "llama3.1:8b");
        assert_eq!(config.models.llm_model, "llama3.1:8b");
    }

    #[test]
    fn test_config_roundtrip() {
        let original = Config {
            models: ModelsConfig {
                embedding_model: "nomic-embed-text".to_string(),
                llm_model: "phi3:mini".to_string(),
            },
            ollama: OllamaConfig {
                url: "http://127.0.0.1:11434".to_string(),
                generate_timeout_secs: 240,
                embedding_timeout_secs: 90,
            },
            index: IndexConfig {
                embedding_dimension: Some(768),
                last_embedding_model: Some("nomic-embed-text".to_string()),
            },
        };

        let toml_str = toml::to_string(&original).unwrap();
        let restored: Config = toml::from_str(&toml_str).unwrap();

        assert_eq!(original, restored);
    }

    #[test]
    fn test_get_config_path_returns_toml_file() {
        let result = get_config_path();
        assert!(result.is_ok());

        let path = result.unwrap();
        assert_eq!(path.file_name().unwrap(), "config.toml");
    }

    #[test]
    fn test_save_and_load_config() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.toml");

        let config = Config {
            models: ModelsConfig {
                embedding_model: "test-embed".to_string(),
                llm_model: "test-llm".to_string(),
            },
            ollama: OllamaConfig {
                url: "http://test:11434".to_string(),
                generate_timeout_secs: 200,
                embedding_timeout_secs: 100,
            },
            index: IndexConfig {
                embedding_dimension: Some(512),
                last_embedding_model: Some("test-embed".to_string()),
            },
        };

        // Serialize and write
        let content = toml::to_string_pretty(&config).unwrap();
        fs::write(&config_path, &content).unwrap();

        // Read and deserialize
        let loaded_content = fs::read_to_string(&config_path).unwrap();
        let loaded_config: Config = toml::from_str(&loaded_content).unwrap();

        assert_eq!(config, loaded_config);
    }

    #[test]
    fn test_needs_index_rebuild() {
        let mut config = Config::default();

        // No previous index
        assert!(!config.needs_index_rebuild());

        // Set index metadata
        config.update_index_metadata(768);
        assert!(!config.needs_index_rebuild());

        // Change embedding model
        config.models.embedding_model = "different-model".to_string();
        assert!(config.needs_index_rebuild());
    }

    #[test]
    fn test_update_index_metadata() {
        let mut config = Config::default();
        config.update_index_metadata(1024);

        assert_eq!(config.index.embedding_dimension, Some(1024));
        assert_eq!(
            config.index.last_embedding_model,
            Some("nomic-embed-text".to_string())
        );
    }

    #[cfg(unix)]
    #[test]
    fn test_file_permissions() {
        use std::os::unix::fs::PermissionsExt;

        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.toml");

        let config = Config::default();
        let content = toml::to_string_pretty(&config).unwrap();
        fs::write(&config_path, &content).unwrap();

        // Set permissions
        let permissions = fs::Permissions::from_mode(0o600);
        fs::set_permissions(&config_path, permissions).unwrap();

        // Verify permissions
        let metadata = fs::metadata(&config_path).unwrap();
        let mode = metadata.permissions().mode() & 0o777;
        assert_eq!(mode, 0o600);
    }
}