models_dev/
traits.rs

1//! Traits for models.dev integration.
2//!
3//! This module defines the ModelsDevAware trait for integrating
4//! with the models.dev API schema.
5
6use crate::types::{Model, Provider};
7
8/// A trait for types that can be integrated with models.dev provider information.
9///
10/// This trait allows provider implementations to declare their compatibility with
11/// models.dev schema and convert from models.dev provider information.
12/// 
13/// # Example
14/// 
15/// ```rust
16/// use models_dev::traits::ModelsDevAware;
17/// use models_dev::types::{Provider, Model};
18/// 
19/// struct MyProvider {
20///     // Provider-specific fields
21/// }
22/// 
23/// impl ModelsDevAware for MyProvider {
24///     fn supported_npm_packages() -> Vec<String> {
25///         vec!["@ai-sdk/my-provider".to_string()]
26///     }
27///     
28///     fn from_models_dev_info(provider: &Provider, model: Option<&Model>) -> Option<Self> {
29        ///         // Check if this provider supports the NPM package
30        ///         if !Self::supported_npm_packages().contains(&provider.npm) {
31        ///             return None;
32        ///         }
33        ///         
34        ///         // Create provider instance from models.dev info
35        ///         Some(MyProvider {
36        ///             // Initialize with provider and model data
37        ///         })
38        ///     }
39/// }
40/// ```
41pub trait ModelsDevAware {
42    /// The NPM package names that this provider supports.
43    ///
44    /// This should return a list of NPM package names that this provider
45    /// implementation is compatible with. For example, an OpenAI provider
46    /// might return `vec!["@ai-sdk/openai"]`.
47    fn supported_npm_packages() -> Vec<String>;
48
49    /// Create an instance of this type from models.dev provider information.
50    ///
51    /// This method should attempt to convert the models.dev provider information
52    /// into an instance of the implementing type. It should return `None` if
53    /// the provider information is not compatible with this implementation.
54    ///
55    /// # Arguments
56    /// * `provider` - The provider information from models.dev API
57    /// * `model` - Optional specific model information to use
58    ///
59    /// # Returns
60    /// * `Some(Self)` if the provider information is compatible
61    /// * `None` if the provider information is not compatible
62    fn from_models_dev_info(provider: &Provider, model: Option<&Model>) -> Option<Self>
63    where
64        Self: Sized;
65}