use std::sync::LazyLock;
use regex::Regex;
use crate::llm::{Backend, Llm};
static GEMINI_MODEL_VERSION_REGEX: LazyLock<Option<Regex>> =
LazyLock::new(|| Regex::new(r"^gemini-(\d+(\.\d+)?)").ok());
pub fn google_variant(llm: &dyn Llm) -> Option<Backend> {
llm.google_variant()
}
pub fn is_gemini_model(model: &str) -> bool {
extract_model_name(model).starts_with("gemini-")
}
pub fn is_gemini1_model(model: &str) -> bool {
extract_gemini_version(model).is_some_and(|v| v < 2.0)
}
pub fn is_gemini25_or_lower(model: &str) -> bool {
extract_gemini_version(model).is_some_and(|v| v <= 2.5)
}
pub fn is_gemini2_or_above(model: &str) -> bool {
extract_gemini_version(model).is_some_and(|v| v >= 2.0)
}
pub fn can_use_output_schema_with_tools(llm: Option<&dyn Llm>) -> bool {
let Some(llm) = llm else {
return false;
};
is_gemini_model(llm.name())
&& google_variant(llm) == Some(Backend::VertexAi)
&& is_gemini2_or_above(llm.name())
}
pub fn is_gemini_api_variant(llm: &dyn Llm) -> bool {
google_variant(llm) == Some(Backend::GeminiApi)
}
pub fn needs_output_schema_processor(llm: Option<&dyn Llm>) -> bool {
let Some(inner) = llm else {
return false;
};
is_gemini_model(inner.name()) && !can_use_output_schema_with_tools(llm)
}
pub fn is_gemini_3x_live(model: &str) -> bool {
let name = extract_model_name(model);
name.starts_with("gemini-3.") && name.contains("-live") && !is_gemini_35_live_translate(model)
}
pub fn is_gemini_35_live_translate(model: &str) -> bool {
extract_model_name(model).starts_with("gemini-3.5-live-translate")
}
fn extract_gemini_version(model: &str) -> Option<f64> {
let name = extract_model_name(model);
let re = GEMINI_MODEL_VERSION_REGEX.as_ref()?;
let caps = re.captures(&name)?;
caps.get(1)?.as_str().parse::<f64>().ok()
}
fn extract_model_name(model: &str) -> String {
model.rsplit('/').next().unwrap_or(model).to_lowercase()
}