Skip to main content

llm_manager/config/
model_config.rs

1use std::collections::HashMap;
2use std::path::PathBuf;
3
4use serde::{Deserialize, Serialize};
5
6use crate::config::config_base_dir;
7use crate::config::ModelOverride;
8use crate::config::store::{load_all_from_dir, move_to_unused, save_yaml};
9
10/// Directory for per-model YAML configs.
11pub fn models_config_dir() -> PathBuf {
12    config_base_dir()
13        .join("llm-manager")
14        .join("models")
15}
16
17/// Directory for unused (deleted) model configs.
18pub fn unused_config_dir() -> PathBuf {
19    config_base_dir()
20        .join("llm-manager")
21        .join("unused")
22}
23
24/// Per-model configuration store.
25///
26/// Each model config is a YAML file stored in `~/.config/llm-manager/models/`.
27/// Files are named `<model_name>.yaml` where `model_name` is the filename
28/// without the `.gguf` extension.
29///
30/// Deleting a model moves its config file to `~/.config/llm-manager/unused/`
31/// instead of removing it, allowing recovery.
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct ModelConfigStore {
34    models_dir: PathBuf,
35    unused_dir: PathBuf,
36    cache: HashMap<String, ModelOverride>,
37}
38
39impl ModelConfigStore {
40    pub fn new() -> Self {
41        let models_dir = models_config_dir();
42        let unused_dir = unused_config_dir();
43        let cache = load_all_from_dir(&models_dir);
44        Self {
45            models_dir,
46            unused_dir,
47            cache,
48        }
49    }
50
51    /// Get the config for a model by name.
52    pub fn get(&self, name: &str) -> Option<&ModelOverride> {
53        self.cache.get(name)
54    }
55
56    /// Save (or update) a model config.
57    pub fn save(&mut self, name: &str, config: &ModelOverride) {
58        save_yaml(name, config, &self.models_dir, &self.unused_dir);
59        self.cache.insert(name.to_string(), config.clone());
60    }
61
62    /// Delete a model config by moving it to the unused directory.
63    pub fn delete(&mut self, name: &str) {
64        move_to_unused(name, &self.models_dir, &self.unused_dir);
65        self.cache.remove(name);
66    }
67
68    /// Get all model config keys.
69    pub fn keys(&self) -> Vec<String> {
70        let mut keys: Vec<String> = self.cache.keys().cloned().collect();
71        keys.sort();
72        keys
73    }
74}
75
76impl Default for ModelConfigStore {
77    fn default() -> Self {
78        Self::new()
79    }
80}