cc-switch 0.2.1

Switch between multiple Claude / Codex configurations. Cross-platform.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use crate::config::{ConfigStorage, validate_alias_name};
use crate::omp::OmpConfiguration;
use crate::omp::env_writer::write_env_file;
use crate::platform::resolve_npm_cli;
use anyhow::{Result, anyhow};
use std::process::Command;

/// Add an OMP configuration
#[allow(clippy::too_many_arguments)]
pub fn handle_omp_add(
    alias_name: String,
    api_key: Option<String>,
    force: bool,
    interactive: bool,
    from_file: Option<String>,
    base_url: Option<String>,
    slow_model: Option<String>,
    smol_model: Option<String>,
    plan_model: Option<String>,
    storage: &mut ConfigStorage,
) -> Result<()> {
    validate_alias_name(&alias_name)?;

    if storage.get_omp_configuration(&alias_name).is_some() {
        if !force {
            eprintln!(
                "Warning: Configuration '{}' already exists. Use --force to overwrite.",
                alias_name
            );
            return Ok(());
        }
        eprintln!("Overwriting existing configuration '{}'", alias_name);
    }

    let config = if let Some(file_path) = from_file {
        parse_omp_env_file(&file_path, &alias_name)?
    } else if interactive {
        parse_interactive_omp_config(&alias_name)?
    } else {
        // Determine auth mode from the provided key and base_url
        let (auth_mode, anthropic_api_key, anthropic_oauth_token, openai_api_key) = if let Some(
            key,
        ) = api_key
        {
            if base_url.is_some() {
                // When base_url is provided, it's an Anthropic-compatible endpoint
                ("anthropic".to_string(), Some(key), None, None)
            } else if key.starts_with("sk-ant-") {
                ("anthropic".to_string(), Some(key), None, None)
            } else {
                // Default to OpenAI for other key formats
                ("openai".to_string(), None, None, Some(key))
            }
        } else {
            anyhow::bail!(
                "API key is required. Use --api-key <key>, --from-file [<path>], or -i for interactive mode."
            )
        };

        OmpConfiguration {
            alias_name: alias_name.clone(),
            auth_mode,
            anthropic_api_key,
            anthropic_oauth_token,
            openai_api_key,
            base_url,
            slow_model,
            smol_model,
            plan_model,
        }
    };

    storage.add_omp_configuration(config.clone());
    storage.save()?;
    crate::omp::env_writer::update_models_yml(&config)?;
    println!("Configuration '{}' added successfully.", alias_name);
    Ok(())
}

/// Parse an existing .env file into an OmpConfiguration
pub fn parse_omp_env_file(file_path: &str, alias_name: &str) -> Result<OmpConfiguration> {
    let content = std::fs::read_to_string(file_path)
        .map_err(|e| anyhow!("Failed to read .env file '{}': {}", file_path, e))?;

    let mut anthropic_api_key = None;
    let mut anthropic_oauth_token = None;
    let mut openai_api_key = None;
    let mut base_url = None;
    let mut slow_model = None;
    let mut smol_model = None;
    let mut plan_model = None;

    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }
        if let Some((key, value)) = trimmed.split_once('=') {
            let key = key.trim();
            let value = value.trim().trim_matches('"').trim_matches('\'');
            match key {
                "ANTHROPIC_API_KEY" => anthropic_api_key = Some(value.to_string()),
                "ANTHROPIC_OAUTH_TOKEN" => anthropic_oauth_token = Some(value.to_string()),
                "OPENAI_API_KEY" => openai_api_key = Some(value.to_string()),
                "ANTHROPIC_BASE_URL" => base_url = Some(value.to_string()),
                "PI_SLOW_MODEL" => slow_model = Some(value.to_string()),
                "PI_SMOL_MODEL" => smol_model = Some(value.to_string()),
                "PI_PLAN_MODEL" => plan_model = Some(value.to_string()),
                _ => {} // ignore unknown keys
            }
        }
    }

    // Determine auth mode from what's present
    let auth_mode = if anthropic_api_key.is_some() || anthropic_oauth_token.is_some() {
        "anthropic"
    } else if openai_api_key.is_some() {
        "openai"
    } else {
        anyhow::bail!(
            "Could not determine auth mode from .env file '{}'. Expected ANTHROPIC_API_KEY, ANTHROPIC_OAUTH_TOKEN, or OPENAI_API_KEY.",
            file_path
        );
    };

    Ok(OmpConfiguration {
        alias_name: alias_name.to_string(),
        auth_mode: auth_mode.to_string(),
        anthropic_api_key,
        anthropic_oauth_token,
        openai_api_key,
        base_url,
        slow_model,
        smol_model,
        plan_model,
    })
}

/// Interactive mode for creating an OMP configuration
fn parse_interactive_omp_config(alias_name: &str) -> Result<OmpConfiguration> {
    use crate::interactive::{read_input, read_sensitive_input};

    let mode_input = read_input("Auth mode (anthropic/openai) [anthropic]: ")?;
    let auth_mode = if mode_input.is_empty() {
        "anthropic".to_string()
    } else {
        mode_input.to_lowercase()
    };

    let (anthropic_api_key, anthropic_oauth_token, openai_api_key) = match auth_mode.as_str() {
        "anthropic" => {
            let key = read_input("Anthropic API key (press Enter to skip): ")?;
            let oauth = read_sensitive_input("Anthropic OAuth token (press Enter to skip): ")?;
            (
                if key.is_empty() { None } else { Some(key) },
                if oauth.is_empty() { None } else { Some(oauth) },
                None,
            )
        }
        "openai" => {
            let key = read_input("OpenAI API key: ")?;
            if key.is_empty() {
                anyhow::bail!("OpenAI API key is required for openai mode.");
            }
            (None, None, Some(key))
        }
        _ => {
            anyhow::bail!(
                "Invalid auth mode '{}'. Must be 'anthropic' or 'openai'.",
                auth_mode
            );
        }
    };

    let base_url = read_input("Base URL (ANTHROPIC_BASE_URL, press Enter to skip): ")?;
    let slow_model = read_input("PI_SLOW_MODEL (press Enter to skip): ")?;
    let smol_model = read_input("PI_SMOL_MODEL (press Enter to skip): ")?;
    let plan_model = read_input("PI_PLAN_MODEL (press Enter to skip): ")?;

    Ok(OmpConfiguration {
        alias_name: alias_name.to_string(),
        auth_mode,
        anthropic_api_key,
        anthropic_oauth_token,
        openai_api_key,
        base_url: if base_url.is_empty() {
            None
        } else {
            Some(base_url)
        },
        slow_model: if slow_model.is_empty() {
            None
        } else {
            Some(slow_model)
        },
        smol_model: if smol_model.is_empty() {
            None
        } else {
            Some(smol_model)
        },
        plan_model: if plan_model.is_empty() {
            None
        } else {
            Some(plan_model)
        },
    })
}
/// List OMP configurations
pub fn handle_omp_list(plain: bool, name: bool, storage: &ConfigStorage) -> Result<()> {
    let configs = storage.omp_configurations.as_ref();

    if configs.is_none() || configs.as_ref().map(|m| m.is_empty()).unwrap_or(true) {
        println!("No OMP configurations found.");
        return Ok(());
    }

    let mut configs: Vec<&OmpConfiguration> = configs.unwrap().values().collect();
    configs.sort_by(|a, b| a.alias_name.cmp(&b.alias_name));

    if name {
        if plain {
            for config in configs {
                println!("{:<20} {}", config.alias_name, config.auth_mode);
            }
        } else {
            let mut json_array = Vec::new();
            for config in &configs {
                json_array.push(serde_json::json!({
                    "alias_name": config.alias_name,
                    "auth_mode": config.auth_mode,
                }));
            }
            println!(
                "{}",
                serde_json::to_string_pretty(&json_array).expect("Should serialize to JSON")
            );
        }
        return Ok(());
    }

    if plain {
        println!("{:<20} {:<12} Key", "Name", "Mode");
        println!("{}", "-".repeat(56));
        for config in &configs {
            let key_display = match config.auth_mode.as_str() {
                "anthropic" => config
                    .anthropic_api_key
                    .as_deref()
                    .or(config.anthropic_oauth_token.as_deref())
                    .map(mask_key)
                    .unwrap_or_else(|| "(none)".to_string()),
                "openai" => config
                    .openai_api_key
                    .as_deref()
                    .map(mask_key)
                    .unwrap_or_else(|| "(none)".to_string()),
                _ => "(unknown)".to_string(),
            };
            println!(
                "{:<20} {:<12} {}",
                config.alias_name, config.auth_mode, key_display
            );
            if let Some(url) = &config.base_url {
                println!("  base_url: {}", url);
            }
        }
    } else {
        let mut json_array = Vec::new();
        for config in &configs {
            let mut json_obj = serde_json::json!({
                "alias_name": config.alias_name,
                "auth_mode": config.auth_mode,
            });

            match config.auth_mode.as_str() {
                "anthropic" => {
                    if let Some(ref key) = config.anthropic_api_key {
                        json_obj["anthropic_api_key"] = serde_json::json!(mask_key(key));
                    }
                    if let Some(ref token) = config.anthropic_oauth_token {
                        json_obj["anthropic_oauth_token"] = serde_json::json!(mask_key(token));
                    }
                }
                "openai" => {
                    if let Some(ref key) = config.openai_api_key {
                        json_obj["openai_api_key"] = serde_json::json!(mask_key(key));
                    }
                }
                _ => {}
            }

            if let Some(ref model) = config.slow_model {
                json_obj["slow_model"] = serde_json::json!(model);
            }
            if let Some(ref model) = config.smol_model {
                json_obj["smol_model"] = serde_json::json!(model);
            }
            if let Some(ref model) = config.plan_model {
                json_obj["plan_model"] = serde_json::json!(model);
            }
            if let Some(url) = &config.base_url {
                json_obj["base_url"] = serde_json::json!(url);
            }

            json_array.push(json_obj);
        }
        println!(
            "{}",
            serde_json::to_string_pretty(&json_array).expect("Should serialize to JSON")
        );
    }

    Ok(())
}

/// Switch to an OMP configuration and launch OMP CLI
pub fn handle_omp_use(
    alias_name: String,
    r#continue: bool,
    resume: Option<String>,
    prompt: Vec<String>,
    storage: &mut ConfigStorage,
) -> Result<()> {
    let config = storage
        .get_omp_configuration(&alias_name)
        .ok_or_else(|| anyhow!("OMP configuration '{}' not found.", alias_name))?
        .clone();

    write_env_file(&config)?;
    crate::omp::env_writer::update_config_yml(&config)?;
    println!(
        "Switched to OMP configuration '{}' (mode: {}).",
        config.alias_name, config.auth_mode
    );

    launch_omp(r#continue, resume, prompt)?;
    Ok(())
}

/// Launch OMP CLI with optional arguments
fn launch_omp(r#continue: bool, resume: Option<String>, prompt: Vec<String>) -> Result<()> {
    let mut cmd = Command::new(resolve_npm_cli("omp"));

    if r#continue {
        cmd.arg("--continue");
    }
    if let Some(session) = resume {
        cmd.arg("--resume").arg(session);
    }
    if !prompt.is_empty() {
        cmd.arg("--prompt").arg(prompt.join(" "));
    }

    match cmd.status() {
        Ok(status) => {
            if !status.success() {
                return Err(anyhow!("OMP exited with status: {}", status));
            }
        }
        Err(e) => {
            return Err(anyhow!(
                "Failed to launch OMP: {}. Make sure OMP is installed and available in your PATH.",
                e
            ));
        }
    }
    Ok(())
}

/// Remove OMP configurations
pub fn handle_omp_remove(alias_names: Vec<String>, storage: &mut ConfigStorage) -> Result<()> {
    let mut _removed_count = 0;
    let mut not_found_aliases = Vec::new();

    for alias in &alias_names {
        if storage.remove_omp_configuration(alias) {
            _removed_count += 1;
            println!("OMP configuration '{}' removed successfully", alias);
        } else {
            not_found_aliases.push(alias.clone());
        }
    }

    storage.save()?;

    if !not_found_aliases.is_empty() {
        for alias in &not_found_aliases {
            println!("OMP configuration '{}' not found, skipping", alias);
        }
    }

    Ok(())
}

/// Enter interactive mode for OMP configuration selection
pub fn handle_omp_interactive(storage: &ConfigStorage) -> Result<()> {
    crate::interactive::handle_omp_interactive_selection(storage)
}

fn mask_key(key: &str) -> String {
    let len = key.len();
    if len <= 8 {
        return key[..len / 2].to_string() + "****";
    }
    format!("{}****{}", &key[..4], &key[len - 4..])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mask_key_short() {
        let masked = mask_key("abcdef");
        assert_eq!(masked, "abc****");
    }

    #[test]
    fn test_mask_key_long() {
        let masked = mask_key("sk-ant-abcdef1234567890");
        assert_eq!(masked, "sk-a****7890");
    }

    #[test]
    fn test_mask_key_very_long() {
        let masked = mask_key("sk-openai-abc123def456ghi789");
        assert_eq!(masked, "sk-o****i789");
    }
}