Skip to main content

gemini_adk_rs/utils/
model_name.rs

1use regex::Regex;
2use std::sync::LazyLock;
3
4static MODEL_PATH_RE: LazyLock<Regex> = LazyLock::new(|| {
5    Regex::new(r"^projects/[^/]+/locations/[^/]+/publishers/[^/]+/models/(.+)$")
6        .expect("MODEL_PATH_RE literal is a valid regex")
7});
8
9/// Regex to extract the major version digit from a gemini model name.
10/// Matches "gemini-" followed by one or more digits (the major version).
11static GEMINI_VERSION_RE: LazyLock<Regex> = LazyLock::new(|| {
12    Regex::new(r"^gemini-(\d+)").expect("GEMINI_VERSION_RE literal is a valid regex")
13});
14
15/// Extract simple model name from a fully-qualified resource path.
16///
17/// If `model_string` matches the pattern
18/// `projects/{project}/locations/{location}/publishers/{publisher}/models/{model}`,
19/// returns just the `{model}` portion. Otherwise returns `model_string` as-is.
20///
21/// # Examples
22///
23/// ```
24/// use gemini_adk_rs::utils::model_name::extract_model_name;
25///
26/// assert_eq!(
27///     extract_model_name("projects/my-proj/locations/us-central1/publishers/google/models/gemini-2.5-flash"),
28///     "gemini-2.5-flash"
29/// );
30/// assert_eq!(extract_model_name("gemini-2.5-flash"), "gemini-2.5-flash");
31/// ```
32pub fn extract_model_name(model_string: &str) -> &str {
33    MODEL_PATH_RE
34        .captures(model_string)
35        .and_then(|caps| caps.get(1))
36        .map(|m| m.as_str())
37        .unwrap_or(model_string)
38}
39
40/// Returns `true` if the model name starts with `"gemini-"`.
41///
42/// The model name is first extracted from any fully-qualified resource path.
43pub fn is_gemini_model(model_string: &str) -> bool {
44    extract_model_name(model_string).starts_with("gemini-")
45}
46
47/// Returns `true` if the model is a Gemini 1.x model (name starts with `"gemini-1"`).
48pub fn is_gemini1_model(model_string: &str) -> bool {
49    extract_model_name(model_string).starts_with("gemini-1")
50}
51
52/// Returns `true` if the model is Gemini 2.x or above.
53///
54/// Parses the major version number from the model name. If the name does not
55/// match the expected `gemini-{major}...` pattern, returns `false`.
56pub fn is_gemini2_or_above(model_string: &str) -> bool {
57    let name = extract_model_name(model_string);
58    GEMINI_VERSION_RE
59        .captures(name)
60        .and_then(|caps| caps.get(1))
61        .and_then(|m| m.as_str().parse::<u32>().ok())
62        .map(|major| major >= 2)
63        .unwrap_or(false)
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn static_regex_literals_compile() {
72        // Forces each `LazyLock`: a bad literal fails here, not at runtime.
73        LazyLock::force(&MODEL_PATH_RE);
74        LazyLock::force(&GEMINI_VERSION_RE);
75    }
76
77    // ── extract_model_name ──────────────────────────────────────────
78
79    #[test]
80    fn extract_from_full_path() {
81        assert_eq!(
82            extract_model_name(
83                "projects/my-proj/locations/us-central1/publishers/google/models/gemini-2.5-flash"
84            ),
85            "gemini-2.5-flash"
86        );
87    }
88
89    #[test]
90    fn extract_from_simple_name() {
91        assert_eq!(extract_model_name("gemini-2.5-flash"), "gemini-2.5-flash");
92    }
93
94    #[test]
95    fn extract_preserves_suffixes() {
96        assert_eq!(
97            extract_model_name("projects/p/locations/l/publishers/pub/models/gemini-1.5-pro-002"),
98            "gemini-1.5-pro-002"
99        );
100    }
101
102    #[test]
103    fn extract_non_gemini_full_path() {
104        assert_eq!(
105            extract_model_name("projects/p/locations/l/publishers/pub/models/custom-model"),
106            "custom-model"
107        );
108    }
109
110    #[test]
111    fn extract_empty_string() {
112        assert_eq!(extract_model_name(""), "");
113    }
114
115    // ── is_gemini_model ─────────────────────────────────────────────
116
117    #[test]
118    fn gemini_model_simple() {
119        assert!(is_gemini_model("gemini-2.5-flash"));
120        assert!(is_gemini_model("gemini-1.5-pro"));
121    }
122
123    #[test]
124    fn non_gemini_model() {
125        assert!(!is_gemini_model("claude-3-opus"));
126        assert!(!is_gemini_model("gpt-4"));
127        assert!(!is_gemini_model("custom-model"));
128    }
129
130    #[test]
131    fn gemini_model_full_path() {
132        assert!(is_gemini_model(
133            "projects/p/locations/l/publishers/pub/models/gemini-2.5-flash"
134        ));
135    }
136
137    // ── is_gemini1_model ────────────────────────────────────────────
138
139    #[test]
140    fn gemini1_models() {
141        assert!(is_gemini1_model("gemini-1.5-pro"));
142        assert!(is_gemini1_model("gemini-1.5-flash"));
143        assert!(is_gemini1_model("gemini-1.0-pro"));
144    }
145
146    #[test]
147    fn gemini1_full_path() {
148        assert!(is_gemini1_model(
149            "projects/p/locations/l/publishers/pub/models/gemini-1.5-pro-002"
150        ));
151    }
152
153    #[test]
154    fn gemini2_not_gemini1() {
155        assert!(!is_gemini1_model("gemini-2.5-flash"));
156        assert!(!is_gemini1_model("gemini-2.0-flash"));
157    }
158
159    #[test]
160    fn non_gemini_not_gemini1() {
161        assert!(!is_gemini1_model("gpt-4"));
162    }
163
164    // ── is_gemini2_or_above ─────────────────────────────────────────
165
166    #[test]
167    fn gemini2_or_above_positive() {
168        assert!(is_gemini2_or_above("gemini-2.5-flash"));
169        assert!(is_gemini2_or_above("gemini-2.0-flash"));
170        assert!(is_gemini2_or_above("gemini-3.0-ultra"));
171    }
172
173    #[test]
174    fn gemini2_or_above_negative() {
175        assert!(!is_gemini2_or_above("gemini-1.5-pro"));
176        assert!(!is_gemini2_or_above("gemini-1.0-pro"));
177    }
178
179    #[test]
180    fn gemini2_or_above_full_path() {
181        assert!(is_gemini2_or_above(
182            "projects/p/locations/l/publishers/pub/models/gemini-2.5-flash"
183        ));
184    }
185
186    #[test]
187    fn gemini2_or_above_non_gemini() {
188        assert!(!is_gemini2_or_above("custom-model"));
189        assert!(!is_gemini2_or_above("gpt-4"));
190    }
191
192    #[test]
193    fn gemini2_or_above_edge_cases() {
194        assert!(!is_gemini2_or_above(""));
195        assert!(!is_gemini2_or_above("gemini-"));
196    }
197}