language_models: Sort Ollama models by name (#31620)

Hello,

This is my first contribution so apologies if I'm not following the
proper process (I haven't seen anything special in
https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md). Also,
I have tested my changes manually, but I could not figure out an easy we
to instantiate a `LanguageModelSelector` in the unit tests, so I didn't
write a test. If you can provide some guidance I'd be happy to write a
test.

---

If the user configured the models with custom names via `display_name`,
we want the ollama models to be sorted based on the name that is
actually displayed.

~~The original issue is only about ollama but this change will also
affect the other providers.~~

Closes #30854

Release Notes:

- Ollama: Changed models to be sorted by name.
This commit is contained in:
little-dude 2025-06-03 17:37:08 +02:00 committed by GitHub
parent 9c2b90fb8f
commit c0397727e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -19,7 +19,7 @@ use serde::{Deserialize, Serialize};
use settings::{Settings, SettingsStore};
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::{collections::BTreeMap, sync::Arc};
use std::{collections::HashMap, sync::Arc};
use ui::{ButtonLike, Indicator, List, prelude::*};
use util::ResultExt;
@ -201,7 +201,7 @@ impl LanguageModelProvider for OllamaLanguageModelProvider {
}
fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
let mut models: BTreeMap<String, ollama::Model> = BTreeMap::default();
let mut models: HashMap<String, ollama::Model> = HashMap::new();
// Add models from the Ollama API
for model in self.state.read(cx).available_models.iter() {
@ -228,7 +228,7 @@ impl LanguageModelProvider for OllamaLanguageModelProvider {
);
}
models
let mut models = models
.into_values()
.map(|model| {
Arc::new(OllamaLanguageModel {
@ -238,7 +238,9 @@ impl LanguageModelProvider for OllamaLanguageModelProvider {
request_limiter: RateLimiter::new(4),
}) as Arc<dyn LanguageModel>
})
.collect()
.collect::<Vec<_>>();
models.sort_by_key(|model| model.name());
models
}
fn load_model(&self, model: Arc<dyn LanguageModel>, cx: &App) {