Skip to main content

recall_echo/
config_cli.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! CLI handlers for `recall-echo config show` and `recall-echo config set`.
6
7use std::path::Path;
8
9use crate::cli_provider::CliSpec;
10use crate::config::{self, Provider};
11use crate::error::RecallError;
12
13const BOLD: &str = "\x1b[1m";
14const DIM: &str = "\x1b[2m";
15const GREEN: &str = "\x1b[32m";
16const RESET: &str = "\x1b[0m";
17
18/// Display current configuration.
19pub fn show(memory_dir: &Path) -> Result<(), RecallError> {
20    let cfg = config::load(memory_dir);
21    let path = config::config_path(memory_dir);
22    let exists = path.exists();
23
24    eprintln!("{BOLD}recall-echo config{RESET}");
25    if exists {
26        eprintln!("{DIM}{}{RESET}\n", path.display());
27    } else {
28        eprintln!("{DIM}(no config file — using defaults){RESET}\n");
29    }
30
31    // Ephemeral
32    eprintln!("{BOLD}[ephemeral]{RESET}");
33    eprintln!("  max_entries = {}", cfg.ephemeral.max_entries);
34
35    // LLM
36    eprintln!("\n{BOLD}[llm]{RESET}");
37    let provider_label = match &cfg.llm.provider {
38        Provider::Openai => "openai (ollama)".to_string(),
39        other => other.to_string(),
40    };
41    eprintln!("  provider = {provider_label}");
42    eprintln!(
43        "  model    = {} {DIM}({}){RESET}",
44        cfg.llm.resolved_model(),
45        if cfg.llm.model.is_empty() {
46            "default"
47        } else {
48            "custom"
49        }
50    );
51    if cfg.llm.provider.is_cli() {
52        show_cli_section(&cfg.llm);
53    } else {
54        eprintln!(
55            "  api_base = {} {DIM}({}){RESET}",
56            cfg.llm.resolved_api_base(),
57            if cfg.llm.api_base.is_empty() {
58                "default"
59            } else {
60                "custom"
61            }
62        );
63    }
64
65    // Pipeline
66    if let Some(ref pipeline) = cfg.pipeline {
67        eprintln!("\n{BOLD}[pipeline]{RESET}");
68        eprintln!(
69            "  docs_dir  = {}",
70            pipeline
71                .docs_dir
72                .as_deref()
73                .unwrap_or("{DIM}(not set){RESET}")
74        );
75        eprintln!("  auto_sync = {}", pipeline.auto_sync.unwrap_or(false));
76    }
77
78    Ok(())
79}
80
81/// Show the resolved agent-CLI call, so a misconfigured vendor is visible
82/// before it is spawned rather than after it fails.
83fn show_cli_section(llm: &config::LlmSection) {
84    eprintln!("\n{BOLD}[llm.cli]{RESET}");
85    match CliSpec::resolve(&llm.provider, &llm.cli) {
86        Ok(spec) => {
87            let preset = llm
88                .cli
89                .preset
90                .or_else(|| llm.provider.default_cli_preset())
91                .map_or_else(|| "custom".to_string(), |p| p.to_string());
92            eprintln!("  preset   = {preset}");
93            eprintln!("  command  = {}", spec.resolve_command());
94            eprintln!(
95                "  timeout  = {}",
96                spec.timeout
97                    .map_or_else(|| "none".to_string(), |t| format!("{}s", t.as_secs()))
98            );
99            eprintln!("  output   = {}", spec.output_mode);
100            let result = if spec.result_json_paths.is_empty() {
101                "raw stdout".to_string()
102            } else {
103                spec.result_json_paths.to_string()
104            };
105            eprintln!("  result   = {result}");
106            if !spec.ndjson_match.is_empty() {
107                eprintln!("  match    = {}", spec.ndjson_match);
108            }
109            eprintln!(
110                "  {DIM}{}{RESET}",
111                spec.argv_preview(&spec.resolve_model(&llm.model))
112            );
113        }
114        Err(err) => eprintln!("  {err}"),
115    }
116}
117
118/// Set a config key and save.
119pub fn set(memory_dir: &Path, key: &str, value: &str) -> Result<(), RecallError> {
120    let mut cfg = config::load(memory_dir);
121    cfg.set_key(key, value)?;
122    config::save(memory_dir, &cfg)?;
123
124    eprintln!("{GREEN}✓{RESET} Set {BOLD}{key}{RESET} = {BOLD}{value}{RESET}");
125
126    // Show what the new provider resolves to — a CLI provider has no API base,
127    // and what matters instead is the call it will make.
128    if key == "llm.provider" || key == "provider" {
129        if cfg.llm.provider.is_cli() {
130            match CliSpec::resolve(&cfg.llm.provider, &cfg.llm.cli) {
131                Ok(spec) => eprintln!(
132                    "  command  → {}",
133                    spec.argv_preview(&spec.resolve_model(&cfg.llm.model))
134                ),
135                Err(err) => eprintln!("  {err}"),
136            }
137        } else {
138            eprintln!("  model    → {}", cfg.llm.resolved_model());
139            eprintln!("  api_base → {}", cfg.llm.resolved_api_base());
140        }
141    }
142
143    Ok(())
144}