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