Skip to main content

roboticus_core/
model.rs

1//! Canonical utilities for parsing model identifiers.
2//!
3//! Model IDs use the format `"provider/model_name"` (e.g., `"openai/gpt-4o"`).
4//! These functions provide a single source of truth for extracting the two
5//! components, replacing 15+ ad hoc `.split('/')` calls scattered across the
6//! workspace.
7
8/// Extract the provider prefix from a model identifier.
9///
10/// ```
11/// assert_eq!(roboticus_core::model::provider_prefix("openai/gpt-4o"), "openai");
12/// assert_eq!(roboticus_core::model::provider_prefix("gpt-4o"), "gpt-4o");
13/// assert_eq!(roboticus_core::model::provider_prefix(""), "unknown");
14/// ```
15pub fn provider_prefix(model_id: &str) -> &str {
16    if model_id.is_empty() {
17        return "unknown";
18    }
19    model_id.split('/').next().unwrap_or("unknown")
20}
21
22/// Extract the model name (without provider prefix) from a model identifier.
23///
24/// ```
25/// assert_eq!(roboticus_core::model::model_name("openai/gpt-4o"), "gpt-4o");
26/// assert_eq!(roboticus_core::model::model_name("gpt-4o"), "gpt-4o");
27/// ```
28pub fn model_name(model_id: &str) -> &str {
29    model_id
30        .split_once('/')
31        .map(|(_, name)| name)
32        .unwrap_or(model_id)
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn provider_prefix_extracts_correctly() {
41        assert_eq!(provider_prefix("openai/gpt-4o"), "openai");
42        assert_eq!(provider_prefix("anthropic/claude-3-opus"), "anthropic");
43        assert_eq!(provider_prefix("local/llama-3"), "local");
44    }
45
46    #[test]
47    fn provider_prefix_handles_edge_cases() {
48        assert_eq!(provider_prefix("gpt-4o"), "gpt-4o");
49        assert_eq!(provider_prefix(""), "unknown");
50        assert_eq!(provider_prefix("a/b/c"), "a");
51    }
52
53    #[test]
54    fn model_name_extracts_correctly() {
55        assert_eq!(model_name("openai/gpt-4o"), "gpt-4o");
56        assert_eq!(model_name("anthropic/claude-3-opus"), "claude-3-opus");
57    }
58
59    #[test]
60    fn model_name_handles_no_prefix() {
61        assert_eq!(model_name("gpt-4o"), "gpt-4o");
62        assert_eq!(model_name(""), "");
63    }
64}