use anyhow::Result;
pub fn is_cloud_configured() -> bool {
get_cloud_api_key().is_some()
}
pub fn setup_cloud_interactive() -> Result<bool> {
println!("\n=== Ollama Cloud Setup ===\n");
println!("Ollama Cloud runs large models on datacenter-grade hardware.");
println!("Cloud models use the :cloud suffix (e.g., kimi-k2-thinking:cloud).\n");
println!("Mermaid reads the key from the OLLAMA_API_KEY environment variable");
println!("and never writes it to disk. To configure it:\n");
println!(" 1. Get an API key at https://ollama.com/cloud");
println!(" 2. Export it in your shell:\n");
println!(" export OLLAMA_API_KEY=<your-key>\n");
println!(" 3. To persist it, add that line to your shell rc (e.g. ~/.bashrc");
println!(" or ~/.zshrc), then start a new shell.\n");
if is_cloud_configured() {
println!("OLLAMA_API_KEY is set — cloud models are available.\n");
} else {
println!("OLLAMA_API_KEY is not set yet; cloud models stay unavailable");
println!("until you set it and re-run mermaid.\n");
}
Ok(is_cloud_configured())
}
pub fn get_cloud_api_key() -> Option<String> {
std::env::var("OLLAMA_API_KEY")
.ok()
.filter(|key| !key.is_empty())
}
pub fn is_cloud_model(model_name: &str) -> bool {
model_name.ends_with(":cloud")
}
pub fn prompt_cloud_setup_if_needed(model_name: &str) -> Result<bool> {
if !is_cloud_model(model_name) {
return Ok(true); }
if is_cloud_configured() {
return Ok(true); }
println!("\nCloud model requested but OLLAMA_API_KEY is not set.");
println!(" Model: {}\n", model_name);
setup_cloud_interactive()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_cloud_model_detects_suffix() {
assert!(is_cloud_model("kimi-k2-thinking:cloud"));
assert!(!is_cloud_model("qwen3-coder:30b"));
assert!(!is_cloud_model("qwen3-coder:480b-cloud"));
}
#[test]
fn get_cloud_api_key_resolves_from_env_only() {
temp_env::with_vars([("OLLAMA_API_KEY", Some("sk-test"))], || {
assert_eq!(get_cloud_api_key().as_deref(), Some("sk-test"));
assert!(is_cloud_configured());
});
temp_env::with_vars([("OLLAMA_API_KEY", None::<&str>)], || {
assert_eq!(get_cloud_api_key(), None);
assert!(!is_cloud_configured());
});
temp_env::with_vars([("OLLAMA_API_KEY", Some(""))], || {
assert_eq!(get_cloud_api_key(), None);
});
}
}