use crate::Error;
use crate::hub::get_hub;
use std::collections::HashSet;
use std::path::Path;
use std::process::Command;
pub const KEY_ENV_VARS: &[&str] = &[
"OPENAI_API_KEY",
"ANTHROPIC_API_KEY",
"GEMINI_API_KEY",
"XAI_API_KEY",
"DEEPSEEK_API_KEY",
"GROQ_API_KEY",
"COHERE_API_KEY",
];
pub fn get_available_api_keys() -> HashSet<String> {
let mut available_keys = HashSet::new();
for &key in KEY_ENV_VARS {
match std::env::var(key) {
Ok(val) if !val.trim().is_empty() => {
available_keys.insert(key.to_string());
}
_ => (), }
}
available_keys
}
pub async fn open_vscode(path: impl AsRef<Path>) {
let path = path.as_ref();
let output = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(["/C", "code", path.to_str().unwrap_or_default()])
.output()
} else {
Command::new("code").arg(path).output()
};
match output {
Ok(output) if output.status.success() => {}
Ok(output) => {
let msg = format!(
"Error opening VSCode:\nstdout: {}\nstderr: {}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
get_hub().publish(Error::Custom(msg)).await;
}
Err(e) => {
let msg = format!("Failed to execute VSCode command: {}", e);
get_hub().publish(Error::Custom(msg)).await;
}
}
}