Skip to content

Commit d0b2d04

Browse files
Copilotbaijumeswani
andcommitted
Rust SDK: Add info() to Model, get_latest_version to Catalog, update docs
Agent-Logs-Url: https://github.com/microsoft/Foundry-Local/sessions/8082508c-1338-48b2-bdd3-6c2c8e35e195 Co-authored-by: baijumeswani <12852605+baijumeswani@users.noreply.github.com>
1 parent 110801c commit d0b2d04

4 files changed

Lines changed: 48 additions & 1 deletion

File tree

sdk/rust/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ let loaded = catalog.get_loaded_models().await?;
127127

128128
### Model Lifecycle
129129

130-
Each `Model` wraps one or more `ModelVariant` entries (different quantizations, hardware targets). The SDK auto-selects the best available variant, preferring cached versions.
130+
Each `Model` wraps one or more variant entries (different quantizations, hardware targets). The SDK auto-selects the best available variant, preferring cached versions.
131131

132132
```rust
133133
let model = catalog.get_model("phi-3.5-mini").await?;

sdk/rust/docs/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ pub struct Catalog { /* private fields */ }
134134
| `get_model_variant` | `async fn get_model_variant(&self, id: &str) -> Result<Arc<ModelVariant>, FoundryLocalError>` | Look up a variant by unique id. |
135135
| `get_cached_models` | `async fn get_cached_models(&self) -> Result<Vec<Arc<ModelVariant>>, FoundryLocalError>` | Return only variants cached on disk. |
136136
| `get_loaded_models` | `async fn get_loaded_models(&self) -> Result<Vec<Arc<ModelVariant>>, FoundryLocalError>` | Return model variants currently loaded in memory. |
137+
| `get_latest_version` | `async fn get_latest_version(&self, model: &Arc<Model>) -> Result<Arc<ModelVariant>, FoundryLocalError>` | Get the latest version of a model variant. Returns the latest variant matching the selected variant's name. |
137138

138139
---
139140

@@ -149,6 +150,7 @@ pub struct Model { /* private fields */ }
149150
|--------|-----------|-------------|
150151
| `alias` | `fn alias(&self) -> &str` | Alias shared by all variants. |
151152
| `id` | `fn id(&self) -> &str` | Unique identifier of the selected variant. |
153+
| `info` | `fn info(&self) -> &ModelInfo` | Full metadata for the selected variant. |
152154
| `variants` | `fn variants(&self) -> &[Arc<ModelVariant>]` | All variants in this model. |
153155
| `selected_variant` | `fn selected_variant(&self) -> &ModelVariant` | Currently selected variant. |
154156
| `select_variant` | `fn select_variant(&self, id: &str) -> Result<(), FoundryLocalError>` | Select a variant by id. |

sdk/rust/src/catalog.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,45 @@ impl Catalog {
188188
.collect())
189189
}
190190

191+
/// Get the latest version of a model.
192+
///
193+
/// This is used to check if a newer version of a model is available in the
194+
/// catalog for download.
195+
///
196+
/// Returns the same `Arc<Model>` if the model already references the latest
197+
/// version.
198+
pub async fn get_latest_version(&self, model: &Arc<Model>) -> Result<Arc<ModelVariant>> {
199+
self.update_models().await?;
200+
201+
let s = self.lock_state()?;
202+
let catalog_model = s.models_by_alias.get(model.alias()).ok_or_else(|| {
203+
FoundryLocalError::ModelOperation {
204+
reason: format!(
205+
"Model with alias '{}' not found in catalog.",
206+
model.alias()
207+
),
208+
}
209+
})?;
210+
211+
let current_name = &model.selected_variant().info().name;
212+
213+
// variants are sorted by version, so the first one matching the name
214+
// is the latest version for that variant.
215+
let latest = catalog_model
216+
.variants()
217+
.iter()
218+
.find(|v| &v.info().name == current_name)
219+
.ok_or_else(|| FoundryLocalError::ModelOperation {
220+
reason: format!(
221+
"Internal error. Mismatch between model (alias:{}) and variant (name:{}).",
222+
model.alias(),
223+
current_name
224+
),
225+
})?;
226+
227+
Ok(Arc::clone(latest))
228+
}
229+
191230
async fn force_refresh(&self) -> Result<()> {
192231
let raw = self
193232
.core

sdk/rust/src/model.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::error::{FoundryLocalError, Result};
1111
use crate::model_variant::ModelVariant;
1212
use crate::openai::AudioClient;
1313
use crate::openai::ChatClient;
14+
use crate::types::ModelInfo;
1415

1516
/// A model groups one or more [`ModelVariant`]s that share the same alias.
1617
///
@@ -102,6 +103,11 @@ impl Model {
102103
&self.alias
103104
}
104105

106+
/// The full [`ModelInfo`] metadata of the selected variant.
107+
pub fn info(&self) -> &ModelInfo {
108+
self.selected_variant().info()
109+
}
110+
105111
/// Unique identifier of the selected variant.
106112
pub fn id(&self) -> &str {
107113
self.selected_variant().id()

0 commit comments

Comments
 (0)