Skip to main content

aptu_core/facade/
models.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Model listing and validation facade functions.
4
5#[cfg(not(target_arch = "wasm32"))]
6use tracing::instrument;
7
8#[cfg(not(target_arch = "wasm32"))]
9use crate::auth::TokenProvider;
10use crate::error::AptuError;
11
12/// Lists available models from a provider API with caching.
13///
14/// This function fetches the list of available models from a provider's API,
15/// with automatic caching and TTL validation. If the cache is valid, it returns
16/// cached data. Otherwise, it fetches from the API and updates the cache.
17///
18/// # Arguments
19///
20/// * `provider` - Token provider for API credentials
21/// * `provider_name` - Name of the provider (e.g., "openrouter", "gemini")
22///
23/// # Returns
24///
25/// A vector of `ModelInfo` structs with available models.
26///
27/// # Errors
28///
29/// Returns an error if:
30/// - Provider is not found
31/// - API request fails
32/// - Response parsing fails
33#[cfg(not(target_arch = "wasm32"))]
34#[instrument(skip(provider), fields(provider_name))]
35pub async fn list_models(
36    provider: &dyn TokenProvider,
37    provider_name: &str,
38) -> crate::Result<Vec<crate::ai::registry::CachedModel>> {
39    use crate::ai::registry::{CachedModelRegistry, ModelRegistry};
40    use crate::cache::cache_dir;
41
42    let cache_dir = cache_dir();
43    let registry =
44        CachedModelRegistry::new(cache_dir, crate::cache::DEFAULT_MODEL_TTL_SECS, provider);
45
46    registry
47        .list_models(provider_name)
48        .await
49        .map_err(|e| AptuError::ModelRegistry {
50            message: format!("Failed to list models: {e}"),
51        })
52}
53
54#[cfg(target_arch = "wasm32")]
55pub async fn list_models(
56    _provider: &dyn crate::auth::TokenProvider,
57    _provider_name: &str,
58) -> crate::Result<Vec<crate::ai::registry::CachedModel>> {
59    crate::facade::wasm_unsupported!("list_models");
60}
61
62/// Validates if a model exists for a provider.
63///
64/// This function checks if a specific model identifier is available for a provider,
65/// using the cached model registry with automatic caching.
66///
67/// # Arguments
68///
69/// * `provider` - Token provider for API credentials
70/// * `provider_name` - Name of the provider (e.g., "openrouter", "gemini")
71/// * `model_id` - Model identifier to validate
72///
73/// # Returns
74///
75/// `true` if the model exists, `false` otherwise.
76///
77/// # Errors
78///
79/// Returns an error if:
80/// - Provider is not found
81/// - API request fails
82/// - Response parsing fails
83#[cfg(not(target_arch = "wasm32"))]
84#[instrument(skip(provider), fields(provider_name, model_id))]
85pub async fn validate_model(
86    provider: &dyn TokenProvider,
87    provider_name: &str,
88    model_id: &str,
89) -> crate::Result<bool> {
90    use crate::ai::registry::{CachedModelRegistry, ModelRegistry};
91    use crate::cache::cache_dir;
92
93    let cache_dir = cache_dir();
94    let registry =
95        CachedModelRegistry::new(cache_dir, crate::cache::DEFAULT_MODEL_TTL_SECS, provider);
96
97    registry
98        .model_exists(provider_name, model_id)
99        .await
100        .map_err(|e| AptuError::ModelRegistry {
101            message: format!("Failed to validate model: {e}"),
102        })
103}
104
105#[cfg(target_arch = "wasm32")]
106pub async fn validate_model(
107    _provider: &dyn crate::auth::TokenProvider,
108    _provider_name: &str,
109    _model_id: &str,
110) -> crate::Result<bool> {
111    crate::facade::wasm_unsupported!("validate_model");
112}