doum_cli/cli/
switch.rs

1use crate::core::SwitchService;
2use anyhow::{Context, Result};
3
4pub async fn handle_switch_command(provider: Option<String>, model: Option<String>) -> Result<()> {
5    use cliclack::{input, select};
6
7    match (provider, model) {
8        // Specific provider and model provided
9        (Some(prov), Some(mdl)) => {
10            SwitchService::switch_to(&prov, &mdl)?;
11            cliclack::outro(format!("Switched to {} - {}", prov, mdl))?;
12            Ok(())
13        }
14        // Interactive selection
15        (None, None) => {
16            cliclack::intro("🔄 Switch Provider & Model")?;
17
18            // Step 1: Select provider
19            let providers = SwitchService::list_providers();
20            let provider_items: Vec<_> = providers
21                .iter()
22                .map(|p| (p.as_str(), p.as_str(), ""))
23                .collect();
24
25            let selected_provider = select("Select provider")
26                .items(&provider_items)
27                .interact()
28                .context("Provider selection failed")?;
29
30            // Step 2: Select model for the chosen provider
31            let models = crate::llm::load_presets(selected_provider);
32            let mut model_items: Vec<_> = models
33                .iter()
34                .map(|m| (m.id.as_str(), m.name.as_str(), m.description.as_str()))
35                .collect();
36
37            // Add custom option
38            model_items.push(("custom", "Custom", "Enter model name manually"));
39
40            let selected_model_id = select("Select model")
41                .items(&model_items)
42                .interact()
43                .context("Model selection failed")?;
44
45            let model = if selected_model_id == "custom" {
46                input("Enter custom model name")
47                    .placeholder("e.g., gpt-4-turbo")
48                    .interact()
49                    .context("Input failed")?
50            } else {
51                selected_model_id.to_string()
52            };
53
54            SwitchService::switch_to(selected_provider, &model)?;
55            cliclack::outro(format!("Switched to {} - {}", selected_provider, model))?;
56            Ok(())
57        }
58        _ => {
59            anyhow::bail!("Usage: doum switch [provider] [model] or just doum switch")
60        }
61    }
62}