use crate::types::Model;
use indexmap::IndexMap;
use serde_json::json;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
pub const PROVIDER: &str = config::providers::CLAUDE_CODE;
pub const API: &str = config::providers::CLAUDE_CODE_API;
pub const NAME: &str = "Claude Code (CLI)";
pub const AUTH_SOURCE: &str = "Claude Code CLI (its own login)";
pub const REQUEST_KEY: &str = "<claude-code-cli>";
const EFFORT_LEVELS: [(&str, &str); 6] = [
("minimal", "low"),
("low", "low"),
("medium", "medium"),
("high", "high"),
("xhigh", "xhigh"),
("max", "max"),
];
fn alias_model(id: &str, name: &str) -> Model {
let effort: serde_json::Map<String, serde_json::Value> =
EFFORT_LEVELS.iter().map(|(k, v)| (k.to_string(), json!(v))).collect();
serde_json::from_value(json!({
"id": id,
"name": name,
"api": API,
"provider": PROVIDER,
"baseUrl": "claude-code://cli",
"reasoning": true,
"thinkingLevelMap": effort,
"input": ["text", "image"],
"cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0},
"contextWindow": 200_000,
"maxTokens": 32_000,
}))
.expect("claude-code models deserialize")
}
pub static MODELS: LazyLock<IndexMap<String, Model>> = LazyLock::new(|| {
[
("claude-opus-5-5", "Claude Opus 5.5 (Claude Code CLI)"),
("claude-fable-5-1", "Claude Fable 5.1 (Claude Code CLI)"),
("claude-fable-5-2", "Claude Fable 5.2 (Claude Code CLI)"),
("claude-haiku-5-5", "Claude Haiku 5.5 (Claude Code CLI)"),
("opus", "Claude Opus (Claude Code CLI)"),
("sonnet", "Claude Sonnet (Claude Code CLI)"),
("haiku", "Claude Haiku (Claude Code CLI)"),
]
.into_iter()
.map(|(id, name)| (id.to_string(), alias_model(id, name)))
.collect()
});
fn path_entries(value: &str) -> impl Iterator<Item = &str> {
let separator = if value.contains(';') && !value.contains(":/") {
';'
} else {
':'
};
value.split(separator).filter(|entry| !entry.is_empty())
}
fn env_value(lookup: &dyn Fn(&str) -> Option<String>, name: &str) -> Option<String> {
lookup(name).filter(|value| !value.trim().is_empty())
}
fn file_exists(path: &str, exists: &dyn Fn(&Path) -> bool, home: Option<&Path>) -> bool {
match (path.strip_prefix('~'), home) {
(Some(rest), Some(home)) => exists(&PathBuf::from(format!("{}{rest}", home.display()))),
_ => exists(Path::new(path)),
}
}
pub fn find_cli(source: &crate::env::EnvSource<'_>) -> Option<String> {
let home = source.home.as_deref();
if let Some(configured) = env_value(source.lookup, crate::claude_code_cli::CLAUDE_CODE_BIN_ENV) {
let configured = configured.trim().to_string();
return file_exists(&configured, source.exists, home).then_some(configured);
}
let path = env_value(source.lookup, "PATH").unwrap_or_default();
for dir in path_entries(&path) {
let dir = dir.trim_end_matches(['/', '\\']);
for name in ["claude", "claude.exe", "claude.cmd"] {
let candidate = format!("{dir}/{name}");
if file_exists(&candidate, source.exists, home) {
return Some(candidate);
}
}
}
None
}
pub fn configured(source: &crate::env::EnvSource<'_>) -> bool {
find_cli(source).is_some()
}