pub fn normalize_model_id(id: &str) -> &str {
let mut s = id;
if s.contains("/publishers/google/models/") {
if let Some(slash) = s.rfind('/') {
s = &s[slash + 1..];
}
}
if let Some((head, dd)) = s.rsplit_once('-') {
if is_n_digits(dd, 2) {
if let Some((head2, mm)) = head.rsplit_once('-') {
if is_n_digits(mm, 2) {
if let Some((head3, tag)) = head2.rsplit_once('-') {
if tag == "preview" || tag == "exp" {
s = head3;
}
}
}
}
}
}
s
}
fn is_n_digits(s: &str, n: usize) -> bool {
s.len() == n && s.bytes().all(|b| b.is_ascii_digit())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn strips_vertex_resource_path() {
assert_eq!(
normalize_model_id("projects/p/locations/us-central1/publishers/google/models/gemini-2.5-pro"),
"gemini-2.5-pro"
);
}
#[test]
fn strips_dated_preview() {
assert_eq!(
normalize_model_id("gemini-2.5-pro-preview-05-06"),
"gemini-2.5-pro"
);
assert_eq!(
normalize_model_id("gemini-2.0-flash-exp-12-01"),
"gemini-2.0-flash"
);
}
#[test]
fn keeps_plain_alias() {
assert_eq!(normalize_model_id("gemini-2.5-pro"), "gemini-2.5-pro");
assert_eq!(normalize_model_id("gemini-2.5-flash"), "gemini-2.5-flash");
}
}