Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
language_models: Log and record model-fetch failures instead of discarding them (#64171)
Somfaf8f0b69802
diff --git a/crates/language_models/src/provider/anthropic.rs b/crates/language_models/src/provider/anthropic.rsindex ca57042fa68..1fb61688745 100644--- a/crates/language_models/src/provider/anthropic.rs+++ b/crates/language_models/src/provider/anthropic.rs@@ -19,6 +19,7 @@ use language_model::{use settings::{Settings, SettingsStore};use std::sync::{Arc, LazyLock};use ui::IconName;+use util::ResultExt;use anthropic::completion::collect_compaction_result;pub use anthropic::completion::{AnthropicEventMapper, AnthropicPromptCacheMode, into_anthropic};@@ -53,7 +54,8 @@ pub struct State {credentials_provider: Arc<dyn CredentialsProvider>,http_client: Arc<dyn HttpClient>,fetched_models: Vec<anthropic::Model>,- fetch_models_task: Option<Task<Result<()>>>,+ fetch_models_task: Option<Task<()>>,+ last_fetch_models_error: Option<String>,}impl State {@@ -133,7 +135,16 @@ impl State {}fn restart_fetch_models_task(&mut self, cx: &mut Context<Self>) {- let task = self.fetch_models(cx);+ let fetch_task = self.fetch_models(cx);+ let task = cx.spawn(async move |this, cx| {+ let result = fetch_task.await;+ this.update(cx, |this, _cx| {+ this.last_fetch_models_error =+ result.as_ref().err().map(|error| format!("{error:#}"));+ })+ .ok();+ result.log_err();+ });self.fetch_models_task.replace(task);}}@@ -172,6 +183,7 @@ impl AnthropicLanguageModelProvider {http_client: http_client.clone(),fetched_models: Vec::new(),fetch_models_task: None,+ last_fetch_models_error: None,}});@@ -847,6 +859,82 @@ mod tests {assert!(body["tools"].is_null());}+ #[gpui::test]+ async fn restart_fetch_models_task_records_error_on_failure(cx: &mut gpui::TestAppContext) {+ let http_client = FakeHttpClient::create(|_request| async move {+ Ok(http_client::Response::builder()+ .status(401)+ .body(AsyncBody::from(+ r#"{"error":{"message":"invalid x-api-key"}}"#,+ ))?)+ });+ let provider = direct_anthropic_test_provider(http_client, cx);++ cx.update(|cx| provider.set_api_key(Some("test-key".to_string()), cx))+ .await+ .unwrap();+ cx.executor().run_until_parked();++ let error = cx.read(|cx| provider.state.read(cx).last_fetch_models_error.clone());+ assert!(+ error.is_some(),+ "a failed model fetch should be recorded, not silently dropped"+ );+ }++ #[gpui::test]+ async fn restart_fetch_models_task_cancels_the_previous_fetch(cx: &mut gpui::TestAppContext) {+ let (release_tx, release_rx) = futures::channel::oneshot::channel();+ let release_rx = Arc::new(Mutex::new(Some(release_rx)));+ let call_count = Arc::new(Mutex::new(0));+ let call_count_for_handler = call_count.clone();+ let http_client = FakeHttpClient::create(move |_request| {+ let release_rx = release_rx.clone();+ let call_count = call_count_for_handler.clone();+ async move {+ let call_index = {+ let mut call_count = call_count.lock().unwrap();+ *call_count += 1;+ *call_count+ };+ if call_index == 1 {+ let release_rx = release_rx.lock().unwrap().take();+ if let Some(release_rx) = release_rx {+ release_rx.await.ok();+ }+ Ok(http_client::Response::builder()+ .status(401)+ .body(AsyncBody::from(r#"{"error":{"message":"stale"}}"#))?)+ } else {+ Ok(http_client::Response::builder()+ .status(200)+ .body(AsyncBody::from(r#"{"data":[]}"#))?)+ }+ }+ });+ let provider = direct_anthropic_test_provider(http_client, cx);++ cx.update(|cx| provider.set_api_key(Some("test-key".to_string()), cx))+ .await+ .unwrap();+ cx.executor().run_until_parked();+ assert_eq!(*call_count.lock().unwrap(), 1);++ cx.update(|cx| provider.set_api_key(Some("test-key".to_string()), cx))+ .await+ .unwrap();+ cx.executor().run_until_parked();++ release_tx.send(()).ok();+ cx.executor().run_until_parked();++ let error = cx.read(|cx| provider.state.read(cx).last_fetch_models_error.clone());+ assert!(+ error.is_none(),+ "the cancelled first fetch's stale 401 must not overwrite the second fetch's success"+ );+ }+fn direct_anthropic_test_provider(http_client: Arc<dyn HttpClient>,cx: &mut gpui::TestAppContext,diff --git a/crates/language_models/src/provider/llama_cpp.rs b/crates/language_models/src/provider/llama_cpp.rsindex 9023cf7ccb5..dc8238b950a 100644--- a/crates/language_models/src/provider/llama_cpp.rs+++ b/crates/language_models/src/provider/llama_cpp.rs@@ -73,7 +73,8 @@ pub struct State {credentials_provider: Arc<dyn CredentialsProvider>,http_client: Arc<dyn HttpClient>,fetched_models: Vec<llama_cpp::Model>,- fetch_model_task: Option<Task<Result<()>>>,+ fetch_model_task: Option<Task<()>>,+ last_fetch_models_error: Option<String>,/// Router-mode task on `/models/sse`; re-runs discovery as models load/unload.model_event_task: Option<Task<()>>,/// Same `Arc` as the provider's; re-discovery keeps these cells in sync.@@ -334,7 +335,16 @@ impl State {}fn restart_fetch_models_task(&mut self, cx: &mut Context<Self>) {- let task = self.fetch_models(cx);+ let fetch_task = self.fetch_models(cx);+ let task = cx.spawn(async move |this, cx| {+ let result = fetch_task.await;+ this.update(cx, |this, _cx| {+ this.last_fetch_models_error =+ result.as_ref().err().map(|error| format!("{error:#}"));+ })+ .ok();+ result.log_err();+ });self.fetch_model_task.replace(task);}}@@ -498,6 +508,7 @@ impl LlamaCppLanguageModelProvider {http_client,fetched_models: Default::default(),fetch_model_task: None,+ last_fetch_models_error: None,model_event_task: None,capability_cells,loading_progress,@@ -1939,4 +1950,37 @@ mod tests {&[None, Some("Bearer loaded-key".to_string())]);}++ #[gpui::test]+ async fn restart_fetch_models_task_records_error_on_failure(cx: &mut TestAppContext) {+ cx.update(|cx| {+ let settings_store = SettingsStore::test(cx);+ cx.set_global(settings_store);+ });++ let http_client = FakeHttpClient::create(|_request| async move {+ Ok(http_client::Response::builder()+ .status(500)+ .body(http_client::AsyncBody::from("server error"))?)+ });+ let credentials_provider = Arc::new(FakeCredentialsProvider {+ api_key: b"test-key".to_vec(),+ });+ let provider = cx+ .update(|cx| LlamaCppLanguageModelProvider::new(http_client, credentials_provider, cx));+ cx.run_until_parked();++ cx.update(|cx| {+ provider+ .state+ .update(cx, |state, cx| state.restart_fetch_models_task(cx))+ });+ cx.run_until_parked();++ let error = cx.read(|cx| provider.state.read(cx).last_fetch_models_error.clone());+ assert!(+ error.is_some(),+ "a failed model fetch should be recorded, not silently dropped"+ );+ }}diff --git a/crates/language_models/src/provider/lmstudio.rs b/crates/language_models/src/provider/lmstudio.rsindex e7b186a96a5..1676c43d53a 100644--- a/crates/language_models/src/provider/lmstudio.rs+++ b/crates/language_models/src/provider/lmstudio.rs@@ -23,6 +23,7 @@ use std::sync::LazyLock;use std::{collections::BTreeMap, sync::Arc};use ui::{ButtonLike, ConfiguredApiCard, Divider, List, ListBulletItem, Tooltip, prelude::*};use ui_input::InputField;+use util::ResultExt;use crate::AllLanguageModelSettings;use language_model::chat_completion::ChatCompletionEventMapper;@@ -55,7 +56,8 @@ pub struct State {credentials_provider: Arc<dyn CredentialsProvider>,http_client: Arc<dyn HttpClient>,available_models: Vec<lmstudio::Model>,- fetch_model_task: Option<Task<Result<()>>>,+ fetch_model_task: Option<Task<()>>,+ last_fetch_models_error: Option<String>,_subscription: Subscription,}@@ -122,7 +124,16 @@ impl State {}fn restart_fetch_models_task(&mut self, cx: &mut Context<Self>) {- let task = self.fetch_models(cx);+ let fetch_task = self.fetch_models(cx);+ let task = cx.spawn(async move |this, cx| {+ let result = fetch_task.await;+ this.update(cx, |this, _cx| {+ this.last_fetch_models_error =+ result.as_ref().err().map(|error| format!("{error:#}"));+ })+ .ok();+ result.log_err();+ });self.fetch_model_task.replace(task);}@@ -207,6 +218,7 @@ impl LmStudioLanguageModelProvider {http_client,available_models: Default::default(),fetch_model_task: None,+ last_fetch_models_error: None,_subscription: subscription,}}),@@ -895,3 +907,66 @@ impl Render for ConfigurationView {)}}++#[cfg(test)]+mod tests {+ use super::*;+ use gpui::TestAppContext;+ use http_client::FakeHttpClient;++ struct TestCredentialsProvider;++ impl CredentialsProvider for TestCredentialsProvider {+ fn read_credentials<'a>(+ &'a self,+ _url: &'a str,+ _cx: &'a AsyncApp,+ ) -> std::pin::Pin<+ Box<dyn std::future::Future<Output = Result<Option<(String, Vec<u8>)>>> + 'a>,+ > {+ Box::pin(async { Ok(None) })+ }++ fn write_credentials<'a>(+ &'a self,+ _url: &'a str,+ _username: &'a str,+ _password: &'a [u8],+ _cx: &'a AsyncApp,+ ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + 'a>> {+ Box::pin(async { Ok(()) })+ }++ fn delete_credentials<'a>(+ &'a self,+ _url: &'a str,+ _cx: &'a AsyncApp,+ ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + 'a>> {+ Box::pin(async { Ok(()) })+ }+ }++ #[gpui::test]+ async fn restart_fetch_models_task_records_error_on_failure(cx: &mut TestAppContext) {+ cx.update(|cx| {+ let settings_store = SettingsStore::test(cx);+ cx.set_global(settings_store);+ });++ let http_client = FakeHttpClient::create(|_request| async move {+ Ok(http_client::Response::builder()+ .status(500)+ .body(http_client::AsyncBody::from("server error"))?)+ });+ let provider = cx.update(|cx| {+ LmStudioLanguageModelProvider::new(http_client, Arc::new(TestCredentialsProvider), cx)+ });+ cx.run_until_parked();++ let error = cx.read(|cx| provider.state.read(cx).last_fetch_models_error.clone());+ assert!(+ error.is_some(),+ "a failed model fetch should be recorded, not silently dropped"+ );+ }+}diff --git a/crates/language_models/src/provider/ollama.rs b/crates/language_models/src/provider/ollama.rsindex 64787780a31..59eb2be05ba 100644--- a/crates/language_models/src/provider/ollama.rs+++ b/crates/language_models/src/provider/ollama.rs@@ -29,6 +29,7 @@ use ui::{ButtonLike, ButtonLink, ConfiguredApiCard, Divider, List, ListBulletItem, Tooltip, prelude::*,};use ui_input::InputField;+use util::ResultExt;use crate::AllLanguageModelSettings;@@ -62,7 +63,8 @@ pub struct State {credentials_provider: Arc<dyn CredentialsProvider>,http_client: Arc<dyn HttpClient>,fetched_models: Vec<ollama::Model>,- fetch_model_task: Option<Task<Result<()>>>,+ fetch_model_task: Option<Task<()>>,+ last_fetch_models_error: Option<String>,}impl State {@@ -187,7 +189,16 @@ impl State {}fn restart_fetch_models_task(&mut self, cx: &mut Context<Self>) {- let task = self.fetch_models(cx);+ let fetch_task = self.fetch_models(cx);+ let task = cx.spawn(async move |this, cx| {+ let result = fetch_task.await;+ this.update(cx, |this, _cx| {+ this.last_fetch_models_error =+ result.as_ref().err().map(|error| format!("{error:#}"));+ })+ .ok();+ result.log_err();+ });self.fetch_model_task.replace(task);}}@@ -231,6 +242,7 @@ impl OllamaLanguageModelProvider {http_client,fetched_models: Default::default(),fetch_model_task: None,+ last_fetch_models_error: None,api_key_state: ApiKeyState::new(Self::api_url(cx), (*API_KEY_ENV_VAR).clone()),credentials_provider,}@@ -1230,4 +1242,65 @@ mod tests {);assert_eq!(model_3b.max_tokens, 6000);}++ struct TestCredentialsProvider;++ impl CredentialsProvider for TestCredentialsProvider {+ fn read_credentials<'a>(+ &'a self,+ _url: &'a str,+ _cx: &'a AsyncApp,+ ) -> Pin<Box<dyn std::future::Future<Output = Result<Option<(String, Vec<u8>)>>> + 'a>>+ {+ Box::pin(async { Ok(None) })+ }++ fn write_credentials<'a>(+ &'a self,+ _url: &'a str,+ _username: &'a str,+ _password: &'a [u8],+ _cx: &'a AsyncApp,+ ) -> Pin<Box<dyn std::future::Future<Output = Result<()>> + 'a>> {+ Box::pin(async { Ok(()) })+ }++ fn delete_credentials<'a>(+ &'a self,+ _url: &'a str,+ _cx: &'a AsyncApp,+ ) -> Pin<Box<dyn std::future::Future<Output = Result<()>> + 'a>> {+ Box::pin(async { Ok(()) })+ }+ }++ #[gpui::test]+ async fn restart_fetch_models_task_records_error_on_failure(cx: &mut gpui::TestAppContext) {+ cx.update(|cx| {+ let settings_store = SettingsStore::test(cx);+ cx.set_global(settings_store);+ });++ let http_client = http_client::FakeHttpClient::create(|_request| async move {+ Ok(http_client::Response::builder()+ .status(500)+ .body(http_client::AsyncBody::from("server error"))?)+ });+ let provider = cx.update(|cx| {+ OllamaLanguageModelProvider::new(http_client, Arc::new(TestCredentialsProvider), cx)+ });++ cx.update(|cx| {+ provider+ .state+ .update(cx, |state, cx| state.restart_fetch_models_task(cx))+ });+ cx.run_until_parked();++ let error = cx.read(|cx| provider.state.read(cx).last_fetch_models_error.clone());+ assert!(+ error.is_some(),+ "a failed model fetch should be recorded, not silently dropped"+ );+ }}diff --git a/crates/language_models/src/provider/open_router.rs b/crates/language_models/src/provider/open_router.rsindex d0220cb33ad..7d77650f233 100644--- a/crates/language_models/src/provider/open_router.rs+++ b/crates/language_models/src/provider/open_router.rs@@ -22,6 +22,7 @@ use settings::{OpenRouterAvailableModel as AvailableModel, Settings, SettingsStouse sha2::{Digest as _, Sha256};use std::sync::{Arc, LazyLock};use ui::IconName;+use util::ResultExt;const PROVIDER_ID: LanguageModelProviderId = LanguageModelProviderId::new("openrouter");const PROVIDER_NAME: LanguageModelProviderName = LanguageModelProviderName::new("OpenRouter");@@ -48,7 +49,8 @@ pub struct State {credentials_provider: Arc<dyn CredentialsProvider>,http_client: Arc<dyn HttpClient>,available_models: Vec<open_router::Model>,- fetch_models_task: Option<Task<Result<(), LanguageModelCompletionError>>>,+ fetch_models_task: Option<Task<()>>,+ last_fetch_models_error: Option<String>,}impl State {@@ -124,10 +126,20 @@ impl State {fn restart_fetch_models_task(&mut self, cx: &mut Context<Self>) {if self.is_authenticated() {- let task = self.fetch_models(cx);+ let fetch_task = self.fetch_models(cx);+ let task = cx.spawn(async move |this, cx| {+ let result = fetch_task.await;+ this.update(cx, |this, _cx| {+ this.last_fetch_models_error =+ result.as_ref().err().map(|error| format!("{error:#}"));+ })+ .ok();+ result.log_err();+ });self.fetch_models_task.replace(task);} else {self.available_models.clear();+ self.last_fetch_models_error = None;}}}@@ -158,6 +170,7 @@ impl OpenRouterLanguageModelProvider {http_client: http_client.clone(),available_models: Vec::new(),fetch_models_task: None,+ last_fetch_models_error: None,}});@@ -1171,4 +1184,65 @@ mod tests {);assert_eq!(reasoning.max_tokens, None);}++ struct TestCredentialsProvider;++ impl CredentialsProvider for TestCredentialsProvider {+ fn read_credentials<'a>(+ &'a self,+ _url: &'a str,+ _cx: &'a AsyncApp,+ ) -> std::pin::Pin<+ Box<dyn std::future::Future<Output = Result<Option<(String, Vec<u8>)>>> + 'a>,+ > {+ Box::pin(async { Ok(None) })+ }++ fn write_credentials<'a>(+ &'a self,+ _url: &'a str,+ _username: &'a str,+ _password: &'a [u8],+ _cx: &'a AsyncApp,+ ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + 'a>> {+ Box::pin(async { Ok(()) })+ }++ fn delete_credentials<'a>(+ &'a self,+ _url: &'a str,+ _cx: &'a AsyncApp,+ ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + 'a>> {+ Box::pin(async { Ok(()) })+ }+ }++ #[gpui::test]+ async fn restart_fetch_models_task_records_error_on_failure(cx: &mut gpui::TestAppContext) {+ cx.update(|cx| {+ let settings_store = SettingsStore::test(cx);+ cx.set_global(settings_store);+ });++ let http_client =+ http_client::FakeHttpClient::create(|_request| async move {+ Ok(http_client::Response::builder().status(401).body(+ http_client::AsyncBody::from(r#"{"error":{"message":"invalid key"}}"#),+ )?)+ });+ let provider = cx.update(|cx| {+ OpenRouterLanguageModelProvider::new(http_client, Arc::new(TestCredentialsProvider), cx)+ });++ cx.update(|cx| provider.set_api_key(Some("test-key".to_string()), cx))+ .await+ .unwrap();+ cx.run_until_parked();++ let error = cx.read(|cx| provider.state.read(cx).last_fetch_models_error.clone());+ assert!(+ error.is_some(),+ "a failed model fetch should be recorded, not silently dropped"+ );+ }}diff --git a/crates/language_models/src/provider/vercel_ai_gateway.rs b/crates/language_models/src/provider/vercel_ai_gateway.rsindex 55ac61edb81..91dc463d205 100644--- a/crates/language_models/src/provider/vercel_ai_gateway.rs+++ b/crates/language_models/src/provider/vercel_ai_gateway.rs@@ -21,6 +21,7 @@ pub use settings::VercelAiGatewayAvailableModel as AvailableModel;use settings::{Settings, SettingsStore};use std::sync::{Arc, LazyLock};use ui::IconName;+use util::ResultExt;const PROVIDER_ID: LanguageModelProviderId = LanguageModelProviderId::new("vercel_ai_gateway");const PROVIDER_NAME: LanguageModelProviderName =@@ -48,7 +49,8 @@ pub struct State {credentials_provider: Arc<dyn CredentialsProvider>,http_client: Arc<dyn HttpClient>,available_models: Vec<AvailableModel>,- fetch_models_task: Option<Task<Result<(), LanguageModelCompletionError>>>,+ fetch_models_task: Option<Task<()>>,+ last_fetch_models_error: Option<String>,}impl State {@@ -115,10 +117,20 @@ impl State {fn restart_fetch_models_task(&mut self, cx: &mut Context<Self>) {if self.is_authenticated() {- let task = self.fetch_models(cx);+ let fetch_task = self.fetch_models(cx);+ let task = cx.spawn(async move |this, cx| {+ let result = fetch_task.await;+ this.update(cx, |this, _cx| {+ this.last_fetch_models_error =+ result.as_ref().err().map(|error| format!("{error:#}"));+ })+ .ok();+ result.log_err();+ });self.fetch_models_task.replace(task);} else {self.available_models = Vec::new();+ self.last_fetch_models_error = None;}}}@@ -148,6 +160,7 @@ impl VercelAiGatewayLanguageModelProvider {http_client: http_client.clone(),available_models: Vec::new(),fetch_models_task: None,+ last_fetch_models_error: None,}});@@ -580,3 +593,77 @@ async fn list_models(Ok(models)}++#[cfg(test)]+mod tests {+ use super::*;++ struct TestCredentialsProvider;++ impl CredentialsProvider for TestCredentialsProvider {+ fn read_credentials<'a>(+ &'a self,+ _url: &'a str,+ _cx: &'a AsyncApp,+ ) -> std::pin::Pin<+ Box<dyn std::future::Future<Output = Result<Option<(String, Vec<u8>)>>> + 'a>,+ > {+ Box::pin(async { Ok(None) })+ }++ fn write_credentials<'a>(+ &'a self,+ _url: &'a str,+ _username: &'a str,+ _password: &'a [u8],+ _cx: &'a AsyncApp,+ ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + 'a>> {+ Box::pin(async { Ok(()) })+ }++ fn delete_credentials<'a>(+ &'a self,+ _url: &'a str,+ _cx: &'a AsyncApp,+ ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + 'a>> {+ Box::pin(async { Ok(()) })+ }+ }++ #[gpui::test]+ async fn restart_fetch_models_task_records_error_on_failure(cx: &mut gpui::TestAppContext) {+ cx.update(|cx| {+ let settings_store = SettingsStore::test(cx);+ cx.set_global(settings_store);+ });++ let http_client = http_client::FakeHttpClient::create(|_request| async move {+ Ok(http_client::Response::builder()+ .status(401)+ .body(AsyncBody::from(r#"{"error":{"message":"invalid key"}}"#))?)+ });+ let provider = cx.update(|cx| {+ VercelAiGatewayLanguageModelProvider::new(+ http_client,+ Arc::new(TestCredentialsProvider),+ cx,+ )+ });++ cx.update(|cx| provider.set_api_key(Some("test-key".to_string()), cx))+ .await+ .unwrap();+ cx.update(|cx| {+ provider+ .state+ .update(cx, |state, cx| state.restart_fetch_models_task(cx))+ });+ cx.run_until_parked();++ let error = cx.read(|cx| provider.state.read(cx).last_fetch_models_error.clone());+ assert!(+ error.is_some(),+ "a failed model fetch should be recorded, not silently dropped"+ );+ }+}