Skip to main content

mermaid_cli/app/
config.rs

1use crate::constants::{DEFAULT_MAX_TOKENS, DEFAULT_OLLAMA_PORT, DEFAULT_TEMPERATURE};
2use crate::models::ReasoningLevel;
3use crate::runtime::{PolicyOverride, SafetyMode};
4use anyhow::{Context, Result};
5use directories::ProjectDirs;
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8use std::path::PathBuf;
9
10/// Main configuration structure
11#[derive(Debug, Clone, Default, Serialize, Deserialize)]
12pub struct Config {
13    /// Last used model (persisted between sessions)
14    #[serde(default)]
15    pub last_used_model: Option<String>,
16
17    /// Default model configuration
18    #[serde(default)]
19    pub default_model: ModelSettings,
20
21    /// Ollama configuration
22    #[serde(default)]
23    pub ollama: OllamaConfig,
24
25    /// Non-interactive mode configuration
26    #[serde(default)]
27    pub non_interactive: NonInteractiveConfig,
28
29    /// MCP server configurations
30    #[serde(default)]
31    pub mcp_servers: HashMap<String, McpServerConfig>,
32
33    /// User overrides + custom OpenAI-compatible providers. Keys are
34    /// provider names; matching a built-in registry entry overrides its
35    /// defaults, anything else defines a fully custom provider.
36    /// Example:
37    /// ```toml
38    /// [providers.groq]
39    /// api_key_env = "MY_GROQ_KEY"  # override default GROQ_API_KEY
40    ///
41    /// [providers.my-vllm]
42    /// base_url = "http://192.168.1.42:8000/v1"
43    /// api_key_env = "VLLM_KEY"
44    /// compat = "openai-effort"
45    /// ```
46    #[serde(default)]
47    pub providers: HashMap<String, UserProviderConfig>,
48
49    /// Per-model reasoning preferences keyed by full model ID
50    /// (`provider/name`). Set when the user runs `/reasoning <level>` or
51    /// Alt+T cycles while using a specific model — the new value sticks
52    /// for that model until changed. Falls back to
53    /// `default_model.reasoning` when no entry exists.
54    /// Example:
55    /// ```toml
56    /// [reasoning_per_model]
57    /// "anthropic/claude-sonnet-4-6" = "high"
58    /// "ollama/qwen3-coder:30b" = "low"
59    /// ```
60    #[serde(default)]
61    pub reasoning_per_model: HashMap<String, ReasoningLevel>,
62
63    /// Named model profiles that agents/plugins can request without
64    /// hardcoding a concrete provider model. Values are full model IDs.
65    /// Example:
66    /// ```toml
67    /// [model_profiles]
68    /// fast = "ollama/qwen3-coder:14b"
69    /// large-context = "openai/gpt-5.2"
70    /// tool-strong = "anthropic/claude-sonnet-4-6"
71    /// vision = "gemini/gemini-2.5-pro"
72    /// cheap = "groq/llama-3.3-70b-versatile"
73    /// ```
74    #[serde(default)]
75    pub model_profiles: HashMap<String, String>,
76
77    /// Runtime safety policy. Defaults to `Ask` so mutations / shell /
78    /// network actions require approval out of the box; users opt into
79    /// `Auto` (LLM-vetted) or `FullAccess` deliberately.
80    #[serde(default)]
81    pub safety: SafetyConfig,
82
83    /// Runtime-only prompt customizations supplied by CLI flags. These are
84    /// deliberately skipped when saving config so one-off agent personas do
85    /// not pollute the user's persistent Mermaid settings.
86    #[serde(skip)]
87    pub prompt: PromptConfig,
88}
89
90#[derive(Debug, Clone, Default)]
91pub struct PromptConfig {
92    pub system_prompt: Option<String>,
93    pub append_system_prompt: Vec<String>,
94}
95
96impl PromptConfig {
97    pub fn render_system_prompt(&self, default_prompt: &str) -> String {
98        let mut rendered = self
99            .system_prompt
100            .as_deref()
101            .unwrap_or(default_prompt)
102            .trim_end()
103            .to_string();
104
105        for extra in &self.append_system_prompt {
106            let extra = extra.trim();
107            if extra.is_empty() {
108                continue;
109            }
110            if !rendered.is_empty() {
111                rendered.push_str("\n\n");
112            }
113            rendered.push_str(extra);
114        }
115
116        rendered
117    }
118
119    pub fn is_customized(&self) -> bool {
120        self.system_prompt.is_some() || !self.append_system_prompt.is_empty()
121    }
122}
123
124#[derive(Debug, Clone, Serialize, Deserialize)]
125#[serde(default)]
126pub struct SafetyConfig {
127    pub mode: SafetyMode,
128    pub checkpoint_on_mutation: bool,
129    #[serde(default)]
130    pub overrides: Vec<PolicyOverride>,
131    /// Model id the `Auto`-mode safety classifier uses to vet borderline
132    /// actions. `None` ⇒ vet with the session's active model. Set this to
133    /// point the vet at a cheaper/faster model than the one driving the work.
134    #[serde(default)]
135    pub auto_classifier_model: Option<String>,
136}
137
138impl Default for SafetyConfig {
139    fn default() -> Self {
140        Self {
141            // Safe-by-default: the first run prompts for approval on
142            // mutations / shell / network rather than silently auto-allowing
143            // everything. FullAccess remains available via config.
144            mode: SafetyMode::Ask,
145            checkpoint_on_mutation: true,
146            overrides: Vec::new(),
147            auto_classifier_model: None,
148        }
149    }
150}
151
152/// User-supplied OpenAI-compatible provider configuration. All fields are
153/// optional — when matching a built-in registry entry, only the supplied
154/// fields override; the rest fall back to the registry defaults. For
155/// fully custom providers, `base_url` and `api_key_env` are required.
156#[derive(Debug, Clone, Default, Serialize, Deserialize)]
157pub struct UserProviderConfig {
158    /// Override base URL for `/chat/completions` (None = use built-in
159    /// registry default; required for fully custom providers).
160    #[serde(default)]
161    pub base_url: Option<String>,
162    /// Env var name to read the API key from (None = use the built-in
163    /// registry default like `GROQ_API_KEY`; required for fully custom
164    /// providers).
165    #[serde(default)]
166    pub api_key_env: Option<String>,
167    /// Extra HTTP headers sent on every request to this provider.
168    #[serde(default)]
169    pub extra_headers: HashMap<String, String>,
170    /// For fully custom providers (no built-in registry entry), declares
171    /// which OpenAI-compatible shape the endpoint speaks. Ignored when
172    /// the provider name matches a built-in registry entry. Values:
173    /// `"openai"` (no reasoning), `"openai-effort"` (`reasoning_effort`
174    /// field), `"openrouter"` (nested `reasoning: {effort}` object).
175    #[serde(default)]
176    pub compat: Option<String>,
177    /// Optional preferred model — surfaced by `mermaid status` and used
178    /// as the default when the user picks this provider with no model
179    /// suffix.
180    #[serde(default)]
181    pub default_model: Option<String>,
182}
183
184/// MCP server configuration
185#[derive(Debug, Clone, Serialize, Deserialize)]
186pub struct McpServerConfig {
187    /// Command to execute (e.g., "npx", "node", "python")
188    pub command: String,
189    /// Command-line arguments
190    #[serde(default)]
191    pub args: Vec<String>,
192    /// Environment variables for the server process
193    #[serde(default)]
194    pub env: HashMap<String, String>,
195}
196
197/// Default model settings
198#[derive(Debug, Clone, Serialize, Deserialize)]
199#[serde(default)]
200pub struct ModelSettings {
201    /// Model provider (ollama, openai, anthropic)
202    pub provider: String,
203    /// Model name
204    pub name: String,
205    /// Temperature for generation
206    pub temperature: f32,
207    /// Maximum tokens to generate
208    pub max_tokens: usize,
209    /// Default reasoning depth used for new sessions when no `--reasoning`
210    /// flag is given. Each adapter snaps this onto the closest level the
211    /// model actually supports via `nearest_effort()`.
212    pub reasoning: ReasoningLevel,
213}
214
215impl Default for ModelSettings {
216    fn default() -> Self {
217        Self {
218            provider: String::new(),
219            name: String::new(),
220            temperature: DEFAULT_TEMPERATURE,
221            max_tokens: DEFAULT_MAX_TOKENS,
222            reasoning: ReasoningLevel::default(),
223        }
224    }
225}
226
227/// Ollama configuration
228#[derive(Debug, Clone, Serialize, Deserialize)]
229#[serde(default)]
230pub struct OllamaConfig {
231    /// Ollama server host
232    pub host: String,
233    /// Ollama server port
234    pub port: u16,
235    /// Ollama cloud API key (for :cloud models)
236    /// Set this to use Ollama's cloud inference service
237    /// Get your key at: https://ollama.com/cloud
238    pub cloud_api_key: Option<String>,
239    /// Number of GPU layers to offload (None = auto, 0 = CPU only, positive = specific count)
240    /// Lower values free up VRAM for larger models at the cost of speed
241    pub num_gpu: Option<i32>,
242    /// Number of CPU threads for processing offloaded layers
243    /// Higher values improve CPU inference speed for large models
244    pub num_thread: Option<i32>,
245    /// Context window size (number of tokens)
246    /// Larger values allow longer conversations but use more memory
247    pub num_ctx: Option<i32>,
248    /// Enable NUMA optimization for multi-CPU systems
249    pub numa: Option<bool>,
250}
251
252impl Default for OllamaConfig {
253    fn default() -> Self {
254        Self {
255            host: String::from("localhost"),
256            port: DEFAULT_OLLAMA_PORT,
257            cloud_api_key: None,
258            num_gpu: None,    // Let Ollama auto-detect
259            num_thread: None, // Let Ollama auto-detect
260            num_ctx: None,    // Use model default
261            numa: None,       // Auto-detect
262        }
263    }
264}
265
266/// Non-interactive mode configuration
267#[derive(Debug, Clone, Serialize, Deserialize)]
268#[serde(default)]
269pub struct NonInteractiveConfig {
270    /// Output format (text, json, markdown)
271    pub output_format: String,
272    /// Maximum tokens to generate
273    pub max_tokens: usize,
274    /// Don't execute agent actions (dry run)
275    pub no_execute: bool,
276}
277
278impl Default for NonInteractiveConfig {
279    fn default() -> Self {
280        Self {
281            output_format: String::from("text"),
282            max_tokens: DEFAULT_MAX_TOKENS,
283            no_execute: false,
284        }
285    }
286}
287
288/// Load configuration from single config file
289/// Priority: config file > defaults (that's it - no merging, no env vars)
290pub fn load_config() -> Result<Config> {
291    let config_path = get_config_path()?;
292
293    if config_path.exists() {
294        let toml_str = std::fs::read_to_string(&config_path)
295            .with_context(|| format!("Failed to read {}", config_path.display()))?;
296        let config: Config = toml::from_str(&toml_str).with_context(|| {
297            format!(
298                "Failed to parse {}. Run 'mermaid init' to regenerate.",
299                config_path.display()
300            )
301        })?;
302        Ok(config)
303    } else {
304        Ok(Config::default())
305    }
306}
307
308/// Get the path to the single config file
309pub fn get_config_path() -> Result<PathBuf> {
310    Ok(get_config_dir()?.join("config.toml"))
311}
312
313/// Get the configuration directory
314pub fn get_config_dir() -> Result<PathBuf> {
315    if let Some(proj_dirs) = ProjectDirs::from("", "", "mermaid") {
316        let config_dir = proj_dirs.config_dir();
317        std::fs::create_dir_all(config_dir)?;
318        Ok(config_dir.to_path_buf())
319    } else {
320        // Fallback to home directory
321        let home = std::env::var("HOME")
322            .or_else(|_| std::env::var("USERPROFILE"))
323            .context("Could not determine home directory")?;
324        let config_dir = PathBuf::from(home).join(".config").join("mermaid");
325        std::fs::create_dir_all(&config_dir)?;
326        Ok(config_dir)
327    }
328}
329
330/// Save configuration to file
331pub fn save_config(config: &Config, path: Option<PathBuf>) -> Result<()> {
332    let path = if let Some(p) = path {
333        p
334    } else {
335        get_config_dir()?.join("config.toml")
336    };
337
338    let toml_string = toml::to_string_pretty(config)?;
339    std::fs::write(&path, toml_string)
340        .with_context(|| format!("Failed to write config to {}", path.display()))?;
341
342    Ok(())
343}
344
345/// Create a default configuration file if it doesn't exist
346pub fn init_config() -> Result<()> {
347    let config_file = get_config_path()?;
348
349    if config_file.exists() {
350        println!("Configuration already exists at: {}", config_file.display());
351    } else {
352        let default_config = Config::default();
353        save_config(&default_config, Some(config_file.clone()))?;
354        println!("Created configuration at: {}", config_file.display());
355    }
356
357    Ok(())
358}
359
360/// Persist the last used model to config file
361pub fn persist_last_model(model: &str) -> Result<()> {
362    let mut config = load_config().unwrap_or_default();
363    config.last_used_model = Some(model.to_string());
364    save_config(&config, None)
365}
366
367/// Persist the user's default reasoning level to config file. Mirrors
368/// `persist_last_model` — used by the `/reasoning` slash command and the
369/// Alt+T cycle handler so the choice survives across sessions.
370pub fn persist_default_reasoning(level: ReasoningLevel) -> Result<()> {
371    let mut config = load_config().unwrap_or_default();
372    config.default_model.reasoning = level;
373    save_config(&config, None)
374}
375
376/// Persist a reasoning level for a specific model ID
377/// (e.g. `anthropic/claude-sonnet-4-6`). The TUI calls this from Alt+T,
378/// `/reasoning <level>`, and the does-not-support-thinking auto-snap so
379/// the choice sticks per-model rather than bleeding into other models on
380/// next session start.
381pub fn persist_reasoning_for_model(model_id: &str, level: ReasoningLevel) -> Result<()> {
382    let mut config = load_config().unwrap_or_default();
383    config
384        .reasoning_per_model
385        .insert(model_id.to_string(), level);
386    save_config(&config, None)
387}
388
389/// Resolve which model to use: CLI arg > last_used > default_model > any available
390pub async fn resolve_model_id(cli_model: Option<&str>, config: &Config) -> anyhow::Result<String> {
391    if let Some(model) = cli_model {
392        if let Some(resolved) = resolve_model_profile_alias(model, config)? {
393            return Ok(resolved);
394        }
395        return Ok(model.to_string());
396    }
397    if let Some(last_model) = &config.last_used_model {
398        if let Some(resolved) = resolve_model_profile_alias(last_model, config)? {
399            return Ok(resolved);
400        }
401        return Ok(last_model.clone());
402    }
403    if !config.default_model.provider.is_empty() && !config.default_model.name.is_empty() {
404        return Ok(format!(
405            "{}/{}",
406            config.default_model.provider, config.default_model.name
407        ));
408    }
409    let available = crate::ollama::require_any_model(config).await?;
410    // `require_any_model` already errors on empty, so this `.first()` is
411    // never `None` in practice. Use `.first()` over `[0]` so the precondition
412    // is enforced by the type system instead of by a comment.
413    let first = available
414        .first()
415        .ok_or_else(|| anyhow::anyhow!("require_any_model returned empty list"))?;
416    Ok(format!("ollama/{}", first))
417}
418
419fn resolve_model_profile_alias(requested: &str, config: &Config) -> anyhow::Result<Option<String>> {
420    let profile = requested.strip_prefix("profile:").unwrap_or(requested);
421    if let Some(model) = config.model_profiles.get(profile) {
422        anyhow::ensure!(
423            !model.trim().is_empty(),
424            "model profile `{}` is configured with an empty model id",
425            profile
426        );
427        return Ok(Some(model.clone()));
428    }
429    if requested.starts_with("profile:") {
430        anyhow::bail!(
431            "model profile `{}` is not configured; add it under [model_profiles]",
432            profile
433        );
434    }
435    Ok(None)
436}
437
438#[cfg(test)]
439mod tests {
440    use super::*;
441
442    /// Configs persisted before Step 4 don't have a `reasoning` field on
443    /// `[default_model]`. Loading them must succeed and yield the
444    /// `Medium` default — otherwise existing user configs break on
445    /// upgrade.
446    #[test]
447    fn model_settings_deserializes_without_reasoning_field() {
448        let toml_blob = r#"
449            provider = "ollama"
450            name = "qwen3-coder:30b"
451            temperature = 0.7
452            max_tokens = 4096
453        "#;
454        let settings: ModelSettings = toml::from_str(toml_blob).expect("backward compat");
455        assert_eq!(settings.reasoning, ReasoningLevel::Medium);
456        assert_eq!(settings.provider, "ollama");
457    }
458
459    #[test]
460    fn model_settings_round_trips_reasoning_high() {
461        let original = ModelSettings {
462            provider: "anthropic".to_string(),
463            name: "claude-sonnet-4-6".to_string(),
464            temperature: 0.5,
465            max_tokens: 8192,
466            reasoning: ReasoningLevel::High,
467        };
468        let toml_blob = toml::to_string(&original).expect("serialize");
469        let back: ModelSettings = toml::from_str(&toml_blob).expect("deserialize");
470        assert_eq!(back.reasoning, ReasoningLevel::High);
471        assert_eq!(back.name, "claude-sonnet-4-6");
472    }
473
474    #[test]
475    fn configured_model_profile_resolves_explicit_alias() {
476        let mut config = Config::default();
477        config
478            .model_profiles
479            .insert("fast".to_string(), "ollama/qwen3-coder:14b".to_string());
480        assert_eq!(
481            resolve_model_profile_alias("fast", &config).unwrap(),
482            Some("ollama/qwen3-coder:14b".to_string())
483        );
484        assert_eq!(
485            resolve_model_profile_alias("profile:fast", &config).unwrap(),
486            Some("ollama/qwen3-coder:14b".to_string())
487        );
488    }
489
490    #[test]
491    fn profile_prefix_requires_configuration() {
492        let config = Config::default();
493        assert!(resolve_model_profile_alias("profile:vision", &config).is_err());
494        assert_eq!(
495            resolve_model_profile_alias("vision", &config).unwrap(),
496            None
497        );
498    }
499
500    /// `persist_default_reasoning` writes to the real config path, so
501    /// this test goes through `save_config(_, Some(path))` directly to
502    /// avoid clobbering the user's actual `~/.config/mermaid/config.toml`.
503    /// Uses `std::env::temp_dir` (matching the pattern in
504    /// `session::conversation` and `utils::logger`) — no external
505    /// `tempfile` crate dependency.
506    #[test]
507    fn save_and_reload_preserves_reasoning_field() {
508        let dir = std::env::temp_dir().join("mermaid_test_config_reasoning");
509        std::fs::create_dir_all(&dir).expect("create temp dir");
510        let path = dir.join("config.toml");
511
512        let mut cfg = Config::default();
513        cfg.default_model.provider = "ollama".to_string();
514        cfg.default_model.name = "qwen3-coder:30b".to_string();
515        cfg.default_model.reasoning = ReasoningLevel::Low;
516
517        save_config(&cfg, Some(path.clone())).expect("save");
518
519        let blob = std::fs::read_to_string(&path).expect("read");
520        let loaded: Config = toml::from_str(&blob).expect("parse back");
521        assert_eq!(loaded.default_model.reasoning, ReasoningLevel::Low);
522
523        let _ = std::fs::remove_dir_all(&dir);
524    }
525
526    /// Per-model entries serialize as a TOML table with quoted keys (the
527    /// model IDs contain `/`). This test verifies the round-trip works
528    /// through both serialization and deserialization, matching what
529    /// `persist_reasoning_for_model` would produce in real use.
530    #[test]
531    fn save_and_reload_preserves_reasoning_per_model_table() {
532        let dir = std::env::temp_dir().join("mermaid_test_config_per_model_reasoning");
533        std::fs::create_dir_all(&dir).expect("create temp dir");
534        let path = dir.join("config.toml");
535
536        let mut cfg = Config::default();
537        cfg.reasoning_per_model.insert(
538            "anthropic/claude-sonnet-4-6".to_string(),
539            ReasoningLevel::High,
540        );
541        cfg.reasoning_per_model
542            .insert("ollama/qwen3-coder:30b".to_string(), ReasoningLevel::Low);
543
544        save_config(&cfg, Some(path.clone())).expect("save");
545
546        let blob = std::fs::read_to_string(&path).expect("read");
547        let loaded: Config = toml::from_str(&blob).expect("parse back");
548        assert_eq!(
549            loaded
550                .reasoning_per_model
551                .get("anthropic/claude-sonnet-4-6"),
552            Some(&ReasoningLevel::High)
553        );
554        assert_eq!(
555            loaded.reasoning_per_model.get("ollama/qwen3-coder:30b"),
556            Some(&ReasoningLevel::Low)
557        );
558
559        let _ = std::fs::remove_dir_all(&dir);
560    }
561
562    /// Configs from before Step 5b don't have a `reasoning_per_model`
563    /// section. Loading them must succeed with an empty map — otherwise
564    /// upgrade breaks every existing user.
565    #[test]
566    fn config_deserializes_without_reasoning_per_model() {
567        let toml_blob = r#"
568            last_used_model = "ollama/qwen3-coder:30b"
569
570            [default_model]
571            provider = "ollama"
572            name = "qwen3-coder:30b"
573            temperature = 0.7
574            max_tokens = 4096
575        "#;
576        let cfg: Config = toml::from_str(toml_blob).expect("backward compat");
577        assert!(cfg.reasoning_per_model.is_empty());
578        assert!(!cfg.prompt.is_customized());
579    }
580
581    #[test]
582    fn prompt_config_replaces_and_appends_without_persisting() {
583        let mut cfg = Config::default();
584        cfg.prompt.system_prompt = Some("base".to_string());
585        cfg.prompt
586            .append_system_prompt
587            .push("extra instructions".to_string());
588
589        assert_eq!(
590            cfg.prompt.render_system_prompt("default"),
591            "base\n\nextra instructions"
592        );
593
594        let blob = toml::to_string(&cfg).expect("serialize");
595        assert!(!blob.contains("extra instructions"));
596        let loaded: Config = toml::from_str(&blob).expect("deserialize");
597        assert!(!loaded.prompt.is_customized());
598    }
599}