Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
agent_ui: Fix reporting of 401/403 errors (#59119)
Tom Houlé9622ae92e12f
diff --git a/crates/agent_ui/src/conversation_view.rs b/crates/agent_ui/src/conversation_view.rsindex bf923b98d67..bf1dca28b83 100644--- a/crates/agent_ui/src/conversation_view.rs+++ b/crates/agent_ui/src/conversation_view.rs@@ -133,17 +133,18 @@ pub(crate) enum ThreadError {provider: SharedString,},PromptTooLarge,- NoApiKey {+ NoCredentials {provider: SharedString,},StreamError {provider: SharedString,},- InvalidApiKey {+ AuthenticationFailed {provider: SharedString,},PermissionDenied {provider: SharedString,+ message: Option<SharedString>,},RequestFailed,MaxOutputTokens,@@ -180,7 +181,7 @@ impl From<anyhow::Error> for ThreadError {}PromptTooLarge { .. } => Self::PromptTooLarge,PaymentRequired => Self::PaymentRequired,- NoApiKey { provider } => Self::NoApiKey {+ NoApiKey { provider } => Self::NoCredentials {provider: provider.to_string().into(),},StreamEndedUnexpectedly { provider }@@ -189,11 +190,12 @@ impl From<anyhow::Error> for ThreadError {| HttpSend { provider, .. } => Self::StreamError {provider: provider.to_string().into(),},- AuthenticationError { provider, .. } => Self::InvalidApiKey {+ AuthenticationError { provider, .. } => Self::AuthenticationFailed {provider: provider.to_string().into(),},- PermissionError { provider, .. } => Self::PermissionDenied {+ PermissionError { provider, message } => Self::PermissionDenied {provider: provider.to_string().into(),+ message: Some(message.clone().into()),},UpstreamProviderError { .. } => Self::RequestFailed,DataRetentionConsentRequired { .. } => Self::DataRetentionConsentRequired,diff --git a/crates/agent_ui/src/conversation_view/thread_view.rs b/crates/agent_ui/src/conversation_view/thread_view.rsindex 6c4dc654621..691e676f8e9 100644--- a/crates/agent_ui/src/conversation_view/thread_view.rs+++ b/crates/agent_ui/src/conversation_view/thread_view.rs@@ -24,7 +24,7 @@ use gpui::TaskExt;use heapless::Vec as ArrayVec;use language_model::{FastModeConfirmation, LanguageModel, LanguageModelEffortLevel, LanguageModelId,- LanguageModelProviderId, LanguageModelRegistry, Speed,+ LanguageModelProvider, LanguageModelProviderId, LanguageModelRegistry, Speed,};use settings::{update_settings_file, update_settings_file_with_completion};use ui::{@@ -1818,28 +1818,30 @@ impl ThreadView {None,"Context too large for the model's context window.".into(),),- ThreadError::NoApiKey { provider } => (+ ThreadError::NoCredentials { provider } => ("no_api_key",None,- format!("No API key configured for {provider}.").into(),+ format!("No credentials configured for {provider}.").into(),),ThreadError::StreamError { provider } => ("stream_error",None,format!("Connection to {provider}'s API was interrupted.").into(),),- ThreadError::InvalidApiKey { provider } => (+ ThreadError::AuthenticationFailed { provider } => ("invalid_api_key",None,- format!("Invalid or expired API key for {provider}.").into(),+ format!("Authentication with {provider} failed.").into(),),- ThreadError::PermissionDenied { provider } => (+ ThreadError::PermissionDenied { provider, message } => ("permission_denied",None,- format!(- "{provider}'s API rejected the request due to insufficient permissions."- )- .into(),+ message.clone().unwrap_or_else(|| {+ format!(+ "{provider}'s API rejected the request due to insufficient permissions."+ )+ .into()+ }),),ThreadError::RequestFailed => ("request_failed",@@ -9510,6 +9512,13 @@ impl ThreadView {rems_from_px(13.)}+ fn provider_by_name(name: &SharedString, cx: &App) -> Option<Arc<dyn LanguageModelProvider>> {+ LanguageModelRegistry::read_global(cx)+ .providers()+ .into_iter()+ .find(|provider| provider.name().0 == *name)+ }+pub(crate) fn render_thread_error(&mut self,window: &mut Window,@@ -9550,17 +9559,14 @@ impl ThreadView {cx,),ThreadError::PromptTooLarge => self.render_prompt_too_large_error(cx),- ThreadError::NoApiKey { provider } => self.render_error_callout(- "API Key Missing",- format!(- "No API key is configured for {provider}. \- Add your key via the Agent Panel settings to continue."- )- .into(),- false,- true,- cx,- ),+ ThreadError::NoCredentials { provider } => {+ let message = Self::provider_by_name(provider, cx)+ .map(|provider| provider.missing_credentials_error_message())+ .unwrap_or_else(|| {+ format!("No credentials are configured for {provider}.").into()+ });+ self.render_error_callout("Credentials Missing", message, false, true, cx)+ }ThreadError::StreamError { provider } => self.render_error_callout("Connection Interrupted",format!(@@ -9572,28 +9578,20 @@ impl ThreadView {true,cx,),- ThreadError::InvalidApiKey { provider } => self.render_error_callout(- "Invalid API Key",- format!(- "The API key for {provider} is invalid or has expired. \- Update your key via the Agent Panel settings to continue."- )- .into(),- false,- false,- cx,- ),- ThreadError::PermissionDenied { provider } => self.render_error_callout(- "Permission Denied",- format!(- "{provider}'s API rejected the request due to insufficient permissions. \- Check that your API key has access to this model."- )- .into(),- false,- false,- cx,- ),+ ThreadError::AuthenticationFailed { provider } => {+ let message = Self::provider_by_name(provider, cx)+ .map(|provider| provider.authentication_error_message())+ .unwrap_or_else(|| format!("Could not authenticate with {provider}.").into());+ self.render_error_callout("Authentication Failed", message, false, false, cx)+ }+ ThreadError::PermissionDenied { provider, message } => {+ let message: SharedString = message.clone().unwrap_or_else(|| {+ format!("{provider} rejected the request due to insufficient permissions.")+ .into()+ });++ self.render_error_callout("Permission Denied", message, false, false, cx)+ }ThreadError::RequestFailed => self.render_error_callout("Request Failed","The request could not be completed after multiple attempts. \diff --git a/crates/language_model/src/language_model.rs b/crates/language_model/src/language_model.rsindex fc91c00bdee..169a55b97f4 100644--- a/crates/language_model/src/language_model.rs+++ b/crates/language_model/src/language_model.rs@@ -307,6 +307,32 @@ pub trait LanguageModelProvider: 'static {) -> AnyView;fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>>;+ /// Copy shown when this provider rejects a request as unauthenticated+ /// (HTTP 401). The default assumes API-key authentication; providers using+ /// other mechanisms (account or subscription based auth) should override+ /// this so users aren't told to check an API key they don't have.+ fn authentication_error_message(&self) -> SharedString {+ format!(+ "The API key for {} is invalid or has expired. \+ Update your key via the Agent Panel settings to continue.",+ self.name().0+ )+ .into()+ }++ /// Copy shown when a request fails because no credentials are configured+ /// for this provider. The default assumes API-key authentication;+ /// providers using other mechanisms (account or subscription based auth)+ /// should override this.+ fn missing_credentials_error_message(&self) -> SharedString {+ format!(+ "No API key is configured for {}. \+ Add your key via the Agent Panel settings to continue.",+ self.name().0+ )+ .into()+ }+/// Copy shown the first time a user enables fast mode for a model from/// this provider. Returning `None` skips the confirmation prompt and lets/// the toggle apply silently.diff --git a/crates/language_models/src/provider/cloud.rs b/crates/language_models/src/provider/cloud.rsindex 47fcdaf3c4c..a3eae8249e6 100644--- a/crates/language_models/src/provider/cloud.rs+++ b/crates/language_models/src/provider/cloud.rs@@ -373,6 +373,16 @@ impl LanguageModelProvider for CloudLanguageModelProvider {Task::ready(Ok(()))}+ fn authentication_error_message(&self) -> SharedString {+ "Failed to sign in with your Zed account (401).".into()+ }++ fn missing_credentials_error_message(&self) -> SharedString {+ "You are not signed in to your Zed account. \+ Sign in to continue."+ .into()+ }+fn fast_mode_confirmation(&self, _cx: &App) -> Option<FastModeConfirmation> {Some(FastModeConfirmation {title: "Enable Fast Mode for Zed?".into(),diff --git a/crates/language_models/src/provider/openai_subscribed.rs b/crates/language_models/src/provider/openai_subscribed.rsindex e6071ea3839..66febda01b3 100644--- a/crates/language_models/src/provider/openai_subscribed.rs+++ b/crates/language_models/src/provider/openai_subscribed.rs@@ -256,6 +256,18 @@ impl LanguageModelProvider for OpenAiSubscribedProvider {self.sign_out(cx)}+ fn authentication_error_message(&self) -> SharedString {+ "Your ChatGPT subscription session is invalid or has expired. \+ Sign in again via the Agent Panel settings to continue."+ .into()+ }++ fn missing_credentials_error_message(&self) -> SharedString {+ "You are not signed in to your ChatGPT account. \+ Sign in via the Agent Panel settings to continue."+ .into()+ }+fn fast_mode_confirmation(&self, _cx: &App) -> Option<FastModeConfirmation> {Some(FastModeConfirmation {title: "Enable Fast Mode for OpenAI?".into(),