Do not read LLM credentials when disable_ai is true (#66029)

Closes https://github.com/zed-industries/zed/issues/65735

Release Notes:

- Fixed LLM credentials being read despite disable_ai being true
This commit is contained in:
Kirill Bulatov
2026-10-23 10:32:56 +00:00
committed by GitHub
parent 2214d0a92e
commit b36b283021
31 changed files with 1063 additions and 235 deletions
Generated
+6
View File
@@ -4237,7 +4237,9 @@ name = "credentials_provider"
version = "0.1.0"
dependencies = [
"anyhow",
"futures 0.3.34",
"gpui",
"parking_lot",
]
[[package]]
@@ -5472,6 +5474,7 @@ dependencies = [
"command_palette_hooks",
"copilot",
"copilot_ui",
"credentials_provider",
"edit_prediction",
"edit_prediction_types",
"editor",
@@ -5481,6 +5484,7 @@ dependencies = [
"gpui",
"indoc",
"language",
"language_model",
"markdown",
"menu",
"multi_buffer",
@@ -5494,6 +5498,7 @@ dependencies = [
"util",
"workspace",
"zed_actions",
"zed_credentials_provider",
"zeta_prompt",
]
@@ -16543,6 +16548,7 @@ dependencies = [
"copilot",
"copilot_ui",
"cpal",
"credentials_provider",
"edit_prediction",
"edit_prediction_ui",
"editor",
+2 -61
View File
@@ -419,19 +419,19 @@ pub struct LanguageModels {
model_list: acp_thread::AgentModelList,
refresh_models_rx: watch::Receiver<()>,
refresh_models_tx: watch::Sender<()>,
_authenticate_all_providers_task: Task<()>,
}
impl LanguageModels {
fn new(cx: &mut App) -> Self {
let (refresh_models_tx, refresh_models_rx) = watch::channel(());
LanguageModelRegistry::global(cx)
.update(cx, |registry, cx| registry.authenticate_providers(cx));
let mut this = Self {
models: HashMap::default(),
model_list: acp_thread::AgentModelList::Grouped(IndexMap::default()),
refresh_models_rx,
refresh_models_tx,
_authenticate_all_providers_task: Self::authenticate_all_language_model_providers(cx),
};
this.refresh_list(cx);
this
@@ -517,65 +517,6 @@ impl LanguageModels {
fn model_id(model: &LanguageModel) -> AgentModelId {
AgentModelId::new(format!("{}/{}", model.provider_id().0, model.id().0))
}
fn authenticate_all_language_model_providers(cx: &mut App) -> Task<()> {
let authenticate_all_providers = LanguageModelRegistry::global(cx)
.read(cx)
.visible_providers()
.iter()
.map(|provider| (provider.id(), provider.name(), provider.authenticate(cx)))
.collect::<Vec<_>>();
cx.spawn(async move |cx| {
for (provider_id, provider_name, authenticate_task) in authenticate_all_providers {
if let Err(err) = authenticate_task.await {
match err {
language_model::AuthenticateError::CredentialsNotFound => {
// Since we're authenticating these providers in the
// background for the purposes of populating the
// language selector, we don't care about providers
// where the credentials are not found.
}
language_model::AuthenticateError::ConnectionRefused => {
// Not logging connection refused errors as they are mostly from LM Studio's noisy auth failures.
// LM Studio only has one auth method (endpoint call) which fails for users who haven't enabled it.
// TODO: Better manage LM Studio auth logic to avoid these noisy failures.
}
_ => {
// Some providers have noisy failure states that we
// don't want to spam the logs with every time the
// language model selector is initialized.
//
// Ideally these should have more clear failure modes
// that we know are safe to ignore here, like what we do
// with `CredentialsNotFound` above.
match provider_id.0.as_ref() {
"lmstudio" | "ollama" => {
// LM Studio and Ollama both make fetch requests to the local APIs to determine if they are "authenticated".
//
// These fail noisily, so we don't log them.
}
"copilot_chat" => {
// Copilot Chat returns an error if Copilot is not enabled, so we don't log those errors.
}
_ => {
log::error!(
"Failed to authenticate provider: {}: {err:#}",
provider_name.0
);
}
}
}
}
}
}
cx.update(|cx| {
LanguageModelRegistry::global(cx)
.update(cx, |registry, cx| registry.refresh_fallback_model(cx))
});
})
}
}
/// Implemented by the UI layer to provide the ability for agent tools to create
+1 -5
View File
@@ -51,7 +51,7 @@ pub fn load_codestral_api_key(cx: &mut App) -> Task<Result<(), AuthenticateError
let credentials_provider = zed_credentials_provider::global(cx);
let api_url = codestral_api_url(cx);
codestral_api_key_state(cx).update(cx, |key_state, cx| {
key_state.load_if_needed(api_url, |s| s, credentials_provider, cx)
key_state.load_cached(api_url, |key_state| key_state, credentials_provider, cx)
})
}
@@ -103,10 +103,6 @@ impl CodestralEditPredictionDelegate {
}
}
pub fn ensure_api_key_loaded(cx: &mut App) {
load_codestral_api_key(cx).detach();
}
/// Uses Codestral's Fill-in-the-Middle API for code completion.
async fn fetch_completion(
http_client: Arc<dyn HttpClient>,
+2
View File
@@ -31,5 +31,7 @@ serde_json.workspace = true
util.workspace = true
[dev-dependencies]
credentials_provider = { workspace = true, features = ["test-support"] }
gpui = { workspace = true, features = ["test-support"] }
http_client = { workspace = true, features = ["test-support"] }
serde_json.workspace = true
+59 -11
View File
@@ -538,6 +538,7 @@ pub struct CopilotChat {
models: Option<Vec<Model>>,
client: Arc<dyn HttpClient>,
credentials_provider: Arc<dyn CredentialsProvider>,
load_task: Option<Task<Result<()>>>,
sign_in_task: Option<Task<()>>,
}
@@ -563,11 +564,14 @@ async fn load_stored_token(
configuration: &CopilotChatConfiguration,
cx: &AsyncApp,
) -> Option<String> {
let (_, token) = credentials_provider
.read_credentials(&configuration.credentials_url(), cx)
.await
.ok()
.flatten()?;
let (_, token) = language_model::read_credentials(
credentials_provider.as_ref(),
&configuration.credentials_url(),
cx,
)
.await
.ok()
.flatten()?;
String::from_utf8(token).ok()
}
@@ -587,7 +591,7 @@ impl CopilotChat {
// Load a previously-stored token (or the one from the environment) and
// fetch models if we end up authenticated.
cx.spawn(async move |this, cx| {
let load_task = cx.spawn(async move |this, cx| {
let (env_token, credentials_provider, configuration) =
this.read_with(cx, |this, _| {
(
@@ -620,8 +624,7 @@ impl CopilotChat {
Self::update_models(&this, cx).await?;
}
anyhow::Ok(())
})
.detach();
});
Self {
status: CopilotChatStatus::Starting,
@@ -631,6 +634,7 @@ impl CopilotChat {
configuration,
client,
credentials_provider,
load_task: Some(load_task),
sign_in_task: None,
}
}
@@ -649,6 +653,7 @@ impl CopilotChat {
return;
}
self.load_task = None;
let client = self.client.clone();
let configuration = self.configuration.clone();
let credentials_url = configuration.credentials_url();
@@ -711,6 +716,7 @@ impl CopilotChat {
self.oauth_token = None;
self.api_endpoint = None;
self.models = None;
self.load_task = None;
self.sign_in_task = None;
self.status = CopilotChatStatus::SignedOut;
cx.notify();
@@ -901,7 +907,7 @@ impl CopilotChat {
let credentials_provider = self.credentials_provider.clone();
let configuration = self.configuration.clone();
cx.spawn(async move |this, cx| {
self.load_task = Some(cx.spawn(async move |this, cx| {
let token = load_stored_token(&credentials_provider, &configuration, cx).await;
let configuration_is_current = this.update(cx, |this, cx| {
if this.configuration != configuration {
@@ -926,8 +932,7 @@ impl CopilotChat {
}
}
Ok::<_, anyhow::Error>(())
})
.detach_and_log_err(cx);
}));
}
}
}
@@ -1228,6 +1233,9 @@ async fn stream_messages(
#[cfg(test)]
mod tests {
use super::*;
use credentials_provider::test_support::FakeCredentialsProvider;
use gpui::TestAppContext;
use http_client::FakeHttpClient;
#[test]
fn test_github_urls() {
@@ -1928,4 +1936,44 @@ mod tests {
"\"none\""
);
}
#[gpui::test]
async fn test_disabled_credential_load_configuration_and_signout(cx: &mut TestAppContext) {
cx.update(|cx| {
language_model::set_credentials_read_gate::<DisabledAi>(
|cx| cx.global::<DisabledAi>().0,
cx,
);
});
for sign_out in [false, true] {
cx.update(|cx| cx.set_global(DisabledAi(true)));
let credentials = Arc::new(FakeCredentialsProvider::default());
let client = FakeHttpClient::create(|_| async { panic!("unexpected HTTP request") });
let mut configuration = CopilotChatConfiguration::default();
let chat = cx.new(|cx| {
let mut chat =
CopilotChat::new(client, credentials.clone(), configuration.clone(), cx);
chat.oauth_token = None;
chat
});
cx.run_until_parked();
configuration.enterprise_uri = Some("https://enterprise.example".to_string());
chat.update(cx, |chat, cx| chat.set_configuration(configuration, cx));
cx.run_until_parked();
assert_eq!(credentials.take_reads(), Vec::<String>::new());
if sign_out {
chat.update(cx, |chat, cx| chat.sign_out(cx))
.await
.expect("sign out");
}
cx.update(|cx| cx.set_global(DisabledAi(false)));
cx.run_until_parked();
let expected = vec!["https://enterprise.example/copilot-agent"; usize::from(!sign_out)];
assert_eq!(credentials.take_reads(), expected);
}
}
struct DisabledAi(bool);
impl Global for DisabledAi {}
}
+5
View File
@@ -13,6 +13,11 @@ path = "src/credentials_provider.rs"
test = false
doctest = false
[features]
test-support = ["dep:futures", "dep:parking_lot"]
[dependencies]
anyhow.workspace = true
futures = { workspace = true, optional = true }
gpui.workspace = true
parking_lot = { workspace = true, optional = true }
@@ -1,3 +1,6 @@
#[cfg(feature = "test-support")]
pub mod test_support;
use std::future::Future;
use std::pin::Pin;
@@ -0,0 +1,124 @@
use crate::CredentialsProvider;
use anyhow::{Result, anyhow};
use futures::{FutureExt, channel::oneshot, future::LocalBoxFuture};
use gpui::AsyncApp;
use parking_lot::Mutex;
use std::collections::HashMap;
#[derive(Default)]
pub struct FakeCredentialsProvider {
pub state: Mutex<FakeCredentials>,
}
#[derive(Default)]
pub struct FakeCredentials {
pub keys: HashMap<String, Vec<u8>>,
pub reads: Vec<String>,
pub fail_reads: bool,
delayed_reads: HashMap<String, oneshot::Receiver<()>>,
delayed_mutation: Option<oneshot::Receiver<()>>,
}
impl FakeCredentialsProvider {
pub fn defer_read(&self, url: &str) -> oneshot::Sender<()> {
let (sender, receiver) = oneshot::channel();
assert!(
self.state
.lock()
.delayed_reads
.insert(url.to_string(), receiver)
.is_none()
);
sender
}
pub fn defer_next_mutation(&self) -> oneshot::Sender<()> {
let (sender, receiver) = oneshot::channel();
assert!(
self.state
.lock()
.delayed_mutation
.replace(receiver)
.is_none()
);
sender
}
pub fn assert_reads(&self, urls: &[&str]) {
assert_eq!(self.state.lock().reads, urls);
}
pub fn take_reads(&self) -> Vec<String> {
let mut reads = std::mem::take(&mut self.state.lock().reads);
reads.sort();
reads
}
}
impl CredentialsProvider for FakeCredentialsProvider {
fn read_credentials<'a>(
&'a self,
url: &'a str,
_cx: &'a AsyncApp,
) -> LocalBoxFuture<'a, Result<Option<(String, Vec<u8>)>>> {
async move {
let (result, delayed_read) = {
let mut state = self.state.lock();
state.reads.push(url.to_string());
let result = if state.fail_reads {
Err(anyhow!("keyring unavailable"))
} else {
Ok(state
.keys
.get(url)
.cloned()
.map(|key| (String::from("Bearer"), key)))
};
(result, state.delayed_reads.remove(url))
};
if let Some(delayed_read) = delayed_read {
delayed_read.await?;
}
result
}
.boxed_local()
}
fn write_credentials<'a>(
&'a self,
url: &'a str,
_username: &'a str,
password: &'a [u8],
_cx: &'a AsyncApp,
) -> LocalBoxFuture<'a, Result<()>> {
let delayed_mutation = self.state.lock().delayed_mutation.take();
async move {
if let Some(delayed_mutation) = delayed_mutation {
delayed_mutation.await?;
}
self.state
.lock()
.keys
.insert(url.to_string(), password.to_vec());
Ok(())
}
.boxed_local()
}
fn delete_credentials<'a>(
&'a self,
url: &'a str,
_cx: &'a AsyncApp,
) -> LocalBoxFuture<'a, Result<()>> {
let delayed_mutation = self.state.lock().delayed_mutation.take();
async move {
if let Some(delayed_mutation) = delayed_mutation {
delayed_mutation.await?;
}
self.state.lock().keys.remove(url);
Ok(())
}
.boxed_local()
}
}
+12 -2
View File
@@ -59,7 +59,12 @@ impl Mercury {
cx: &mut Context<EditPredictionStore>,
) -> Task<Result<Option<EditPredictionResult>>> {
self.api_token.update(cx, |key_state, cx| {
_ = key_state.load_if_needed(MERCURY_CREDENTIALS_URL, |s| s, credentials_provider, cx);
_ = key_state.load_cached(
MERCURY_CREDENTIALS_URL,
|key_state| key_state,
credentials_provider,
cx,
);
});
let Some(api_token) = self.api_token.read(cx).key(&MERCURY_CREDENTIALS_URL) else {
return Task::ready(Ok(None));
@@ -399,7 +404,12 @@ pub fn mercury_api_token(cx: &mut App) -> Entity<ApiKeyState> {
pub fn load_mercury_api_token(cx: &mut App) -> Task<Result<(), language_model::AuthenticateError>> {
let credentials_provider = zed_credentials_provider::global(cx);
mercury_api_token(cx).update(cx, |key_state, cx| {
key_state.load_if_needed(MERCURY_CREDENTIALS_URL, |s| s, credentials_provider, cx)
key_state.load_cached(
MERCURY_CREDENTIALS_URL,
|key_state| key_state,
credentials_provider,
cx,
)
})
}
@@ -45,7 +45,7 @@ pub fn load_open_ai_compatible_api_token(
let credentials_provider = zed_credentials_provider::global(cx);
let api_url = open_ai_compatible_api_url(cx);
open_ai_compatible_api_token(cx).update(cx, |key_state, cx| {
key_state.load_if_needed(api_url, |s| s, credentials_provider, cx)
key_state.load_cached(api_url, |key_state| key_state, credentials_provider, cx)
})
}
+3
View File
@@ -47,10 +47,13 @@ zeta_prompt.workspace = true
[dev-dependencies]
copilot = { workspace = true, features = ["test-support"] }
credentials_provider = { workspace = true, features = ["test-support"] }
editor = { workspace = true, features = ["test-support"] }
futures.workspace = true
indoc.workspace = true
language_model.workspace = true
project = { workspace = true, features = ["test-support"] }
workspace = { workspace = true, features = ["test-support"] }
zed_credentials_provider.workspace = true
@@ -1,7 +1,6 @@
use anyhow::Result;
use client::{Client, UserStore, zed_urls};
use cloud_llm_client::UsageLimit;
use codestral::{self, CodestralEditPredictionDelegate};
use copilot::Status;
use edit_prediction::EditPredictionStore;
use edit_prediction_types::EditPredictionDelegateHandle;
@@ -575,28 +574,25 @@ impl EditPredictionButton {
cx.observe(&copilot, |_, _, cx| cx.notify()).detach()
}
cx.observe_global::<SettingsStore>(move |_, cx| cx.notify())
.detach();
cx.observe_global::<SettingsStore>(move |_, cx| {
load_provider_credentials(cx);
cx.notify();
})
.detach();
cx.observe_global::<EditPredictionStore>(move |_, cx| cx.notify())
.detach();
cx.observe(&user_store, |_, _, cx| cx.notify()).detach();
let mercury_api_token_task = edit_prediction::mercury::load_mercury_api_token(cx);
let open_ai_compatible_api_token_task =
edit_prediction::open_ai_compatible::load_open_ai_compatible_api_token(cx);
cx.spawn(async move |this, cx| {
_ = futures::join!(mercury_api_token_task, open_ai_compatible_api_token_task);
this.update(cx, |_, cx| {
cx.notify();
})
.ok();
})
.detach();
CodestralEditPredictionDelegate::ensure_api_key_loaded(cx);
load_provider_credentials(cx);
for key_state in [
edit_prediction::mercury::mercury_api_token(cx),
edit_prediction::open_ai_compatible::open_ai_compatible_api_token(cx),
codestral::codestral_api_key_state(cx),
] {
cx.observe(&key_state, |_, _, cx| cx.notify()).detach();
}
Self {
editor_subscription: None,
@@ -1767,6 +1763,13 @@ fn emit_edit_prediction_menu_opened(
);
}
fn load_provider_credentials(cx: &mut App) {
let _mercury_task = edit_prediction::mercury::load_mercury_api_token(cx);
let _open_ai_compatible_task =
edit_prediction::open_ai_compatible::load_open_ai_compatible_api_token(cx);
let _codestral_task = codestral::load_codestral_api_key(cx);
}
fn copilot_settings_url(enterprise_uri: Option<&str>) -> Arc<str> {
match enterprise_uri {
Some(uri) => format!("{}{}", uri.trim_end_matches('/'), COPILOT_SETTINGS_PATH).into(),
@@ -1778,8 +1781,84 @@ fn copilot_settings_url(enterprise_uri: Option<&str>) -> Arc<str> {
mod tests {
use super::*;
use anyhow::Context as _;
use credentials_provider::test_support::FakeCredentialsProvider;
use gpui::TestAppContext;
use language_model::{ApiKeyState, EnvVar};
use settings::RootUserSettings;
use workspace::AppState;
use zed_credentials_provider::ZedCredentialsProvider;
#[gpui::test]
async fn test_credentials_survive_button_recreation(cx: &mut TestAppContext) {
let app_state = cx.update(AppState::test);
let project = Project::test(app_state.fs.clone(), [], cx).await;
let credentials = Arc::new(FakeCredentialsProvider::default());
cx.update(|cx| {
cx.set_global(ZedCredentialsProvider(credentials.clone()));
language_model::set_credentials_read_gate::<SettingsStore>(
|cx| DisableAiSettings::get_global(cx).disable_ai,
cx,
);
let settings = r#"{"disable_ai":true,"edit_predictions":{
"open_ai_compatible_api":{"api_url":"https://edit.example","model":"test"}
}}"#;
cx.update_global::<SettingsStore, _>(|store, cx| {
store
.set_user_settings(settings, cx)
.expect("edit prediction settings");
});
let env_var = EnvVar {
name: SharedString::from("TEST_API_KEY"),
value: None,
};
for key_state in [
edit_prediction::mercury::mercury_api_token(cx),
edit_prediction::open_ai_compatible::open_ai_compatible_api_token(cx),
codestral::codestral_api_key_state(cx),
] {
key_state.update(cx, |state, _| {
*state = ApiKeyState::new(state.url.clone(), env_var.clone());
});
}
});
let new_button = |cx: &mut TestAppContext| {
cx.new(|cx| {
EditPredictionButton::new(
app_state.fs.clone(),
app_state.user_store.clone(),
PopoverMenuHandle::default(),
project.clone(),
cx,
)
})
};
let button = new_button(cx);
cx.run_until_parked();
assert_eq!(credentials.take_reads(), Vec::<String>::new());
drop(button);
let button = new_button(cx);
cx.run_until_parked();
assert_eq!(credentials.take_reads(), Vec::<String>::new());
cx.update_global::<SettingsStore, _>(|store, cx| {
store.update_user_settings(cx, |settings| {
settings.project.disable_ai = Some(false.into());
});
});
cx.run_until_parked();
assert_eq!(
credentials.take_reads(),
[
"https://api.inceptionlabs.ai/v1/edit/completions",
"https://codestral.mistral.ai",
"https://edit.example",
]
);
drop(button);
let _button = new_button(cx);
cx.run_until_parked();
assert_eq!(credentials.take_reads(), Vec::<String>::new());
}
#[test]
fn test_disabled_globs_selection_and_replacement() -> Result<()> {
+1
View File
@@ -34,4 +34,5 @@ serde_json.workspace = true
thiserror.workspace = true
[dev-dependencies]
credentials_provider = { workspace = true, features = ["test-support"] }
gpui = { workspace = true, features = ["test-support"] }
+284 -39
View File
@@ -1,8 +1,8 @@
use anyhow::{Result, anyhow};
use credentials_provider::CredentialsProvider;
use env_var::EnvVar;
use futures::{FutureExt, future};
use gpui::{AsyncApp, Context, SharedString, Task};
use futures::{FutureExt, channel::oneshot, future};
use gpui::{App, AppContext, AsyncApp, Context, Global, SharedString, Subscription, Task};
use gpui_util::ResultExt as _;
use std::{
fmt::{Display, Formatter},
@@ -20,11 +20,12 @@ pub struct ApiKeyState {
pub url: SharedString,
env_var: EnvVar,
load_status: LoadStatus,
load_task: Option<future::Shared<Task<()>>>,
load_task: Option<ApiKeyLoad>,
}
#[derive(Debug, Clone)]
pub enum LoadStatus {
NotLoaded,
NotPresent,
Error(String),
Loaded(ApiKey),
@@ -36,12 +37,47 @@ pub struct ApiKey {
key: Arc<str>,
}
pub fn set_credentials_read_gate<G: Global>(is_disabled: fn(&App) -> bool, cx: &mut App) {
cx.set_global(CredentialsReadGate {
is_disabled,
subscribe: |cx, is_disabled, sender| {
let mut sender = Some(sender);
cx.observe_global::<G>(move |cx| {
if !is_disabled(cx)
&& let Some(sender) = sender.take()
{
sender.send(()).ok();
}
})
},
});
}
pub async fn read_credentials(
provider: &dyn CredentialsProvider,
url: &str,
cx: &AsyncApp,
) -> Result<Option<(String, Vec<u8>)>> {
while let Some((receiver, _subscription)) = cx.update(|cx| {
let gate = *cx.try_global::<CredentialsReadGate>()?;
if !(gate.is_disabled)(cx) {
return None;
}
let (sender, receiver) = oneshot::channel();
let subscription = (gate.subscribe)(cx, gate.is_disabled, sender);
Some((receiver, subscription))
}) {
receiver.await?;
}
provider.read_credentials(url, cx).await
}
impl ApiKeyState {
pub fn new(url: SharedString, env_var: EnvVar) -> Self {
Self {
url,
env_var,
load_status: LoadStatus::NotPresent,
load_status: LoadStatus::NotLoaded,
load_task: None,
}
}
@@ -102,13 +138,20 @@ impl ApiKeyState {
key: Option<String>,
get_this: impl Fn(&mut Ent) -> &mut Self + 'static,
provider: Arc<dyn CredentialsProvider>,
cx: &Context<Ent>,
cx: &mut Context<Ent>,
) -> Task<Result<()>> {
if self.is_from_env_var() {
return Task::ready(Err(anyhow!(
"bug: attempted to store API key in system keychain when API key is from env var",
)));
}
if self.url != url {
self.url = url.clone();
self.load_status = LoadStatus::NotLoaded;
self.load_task = None;
cx.notify();
}
cx.spawn(async move |ent, cx| {
if let Some(key) = &key {
provider
@@ -119,9 +162,12 @@ impl ApiKeyState {
provider.delete_credentials(&url, cx).await.log_err();
}
ent.update(cx, |ent, cx| {
let this = get_this(ent);
this.url = url;
this.load_status = match &key {
let key_state = get_this(ent);
if key_state.url != url {
return;
}
key_state.load_task = None;
key_state.load_status = match &key {
Some(key) => LoadStatus::Loaded(ApiKey {
source: ApiKeySource::SystemKeychain,
key: key.as_str().into(),
@@ -140,7 +186,7 @@ impl ApiKeyState {
pub fn handle_url_change<Ent: 'static>(
&mut self,
url: SharedString,
get_this: impl Fn(&mut Ent) -> &mut Self + Clone + 'static,
get_this: impl Fn(&mut Ent) -> &mut Self + 'static,
provider: Arc<dyn CredentialsProvider>,
cx: &mut Context<Ent>,
) {
@@ -152,6 +198,22 @@ impl ApiKeyState {
}
}
pub fn load_cached<Ent: 'static>(
&mut self,
url: SharedString,
get_this: impl Fn(&mut Ent) -> &mut Self + 'static,
provider: Arc<dyn CredentialsProvider>,
cx: &mut Context<Ent>,
) -> Task<Result<(), AuthenticateError>> {
if let LoadStatus::NotPresent | LoadStatus::Error(_) = &self.load_status
&& self.url == url
{
return Task::ready(Ok(()));
}
self.load_if_needed(url, get_this, provider, cx)
}
/// If needed, loads the API key associated with the given URL from the system keychain. When a
/// non-empty environment variable is provided, it will be used instead. If called when an API
/// key was already loaded for a different URL, that key will be cleared before loading.
@@ -160,7 +222,7 @@ impl ApiKeyState {
pub fn load_if_needed<Ent: 'static>(
&mut self,
url: SharedString,
get_this: impl Fn(&mut Ent) -> &mut Self + Clone + 'static,
get_this: impl Fn(&mut Ent) -> &mut Self + 'static,
provider: Arc<dyn CredentialsProvider>,
cx: &mut Context<Ent>,
) -> Task<Result<(), AuthenticateError>> {
@@ -181,23 +243,22 @@ impl ApiKeyState {
return Task::ready(Ok(()));
}
let task = if let Some(load_task) = &self.load_task {
load_task.clone()
let completion = if let Some(load_task) = &self.load_task
&& self.url == url
{
load_task.completion.clone()
} else {
let load_task = Self::load(url.clone(), get_this.clone(), provider, cx).shared();
let load_task = Self::load(url.clone(), get_this, provider, cx);
let completion = load_task.completion.clone();
self.url = url;
self.load_status = LoadStatus::NotPresent;
self.load_task = Some(load_task.clone());
self.load_status = LoadStatus::NotLoaded;
self.load_task = Some(load_task);
cx.notify();
load_task
completion
};
cx.spawn(async move |ent, cx| {
task.await;
ent.update(cx, |ent, _cx| {
get_this(ent).load_status.clone().into_authenticate_result()
})
.ok();
cx.background_spawn(async move {
completion.await.ok();
Ok(())
})
}
@@ -207,21 +268,25 @@ impl ApiKeyState {
get_this: impl Fn(&mut Ent) -> &mut Self + 'static,
provider: Arc<dyn CredentialsProvider>,
cx: &Context<Ent>,
) -> Task<()> {
cx.spawn({
async move |ent, cx| {
let load_status =
ApiKey::load_from_system_keychain_impl(&url, provider.as_ref(), cx).await;
ent.update(cx, |ent, cx| {
let this = get_this(ent);
this.url = url;
this.load_status = load_status;
this.load_task = None;
cx.notify();
})
.ok();
}
})
) -> ApiKeyLoad {
let (sender, completion) = oneshot::channel();
let task = cx.spawn(async move |ent, cx| {
let load_status =
ApiKey::load_from_system_keychain_impl(&url, provider.as_ref(), cx).await;
ent.update(cx, |ent, cx| {
let key_state = get_this(ent);
key_state.url = url;
key_state.load_status = load_status;
key_state.load_task = None;
cx.notify();
})
.ok();
sender.send(()).ok();
});
ApiKeyLoad {
_task: task,
completion: completion.shared(),
}
}
}
@@ -255,7 +320,7 @@ impl ApiKey {
if url.is_empty() {
return LoadStatus::NotPresent;
}
let read_result = credentials_provider.read_credentials(&url, cx).await;
let read_result = read_credentials(credentials_provider, url, cx).await;
let api_key = match read_result {
Ok(Some((_, api_key))) => api_key,
Ok(None) => return LoadStatus::NotPresent,
@@ -276,12 +341,27 @@ impl LoadStatus {
fn into_authenticate_result(self) -> Result<ApiKey, AuthenticateError> {
match self {
LoadStatus::Loaded(api_key) => Ok(api_key),
LoadStatus::NotPresent => Err(AuthenticateError::CredentialsNotFound),
LoadStatus::NotLoaded | LoadStatus::NotPresent => {
Err(AuthenticateError::CredentialsNotFound)
}
LoadStatus::Error(err) => Err(AuthenticateError::Other(anyhow!(err))),
}
}
}
#[derive(Clone, Copy)]
struct CredentialsReadGate {
is_disabled: fn(&App) -> bool,
subscribe: fn(&mut App, fn(&App) -> bool, oneshot::Sender<()>) -> Subscription,
}
impl Global for CredentialsReadGate {}
struct ApiKeyLoad {
_task: Task<()>,
completion: future::Shared<oneshot::Receiver<()>>,
}
#[derive(Debug, Clone)]
enum ApiKeySource {
EnvVar(SharedString),
@@ -296,3 +376,168 @@ impl Display for ApiKeySource {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use credentials_provider::test_support::FakeCredentialsProvider;
use gpui::{Entity, TestAppContext};
const URL: &str = "provider";
const OTHER_URL: &str = "other-provider";
#[gpui::test]
async fn test_cached_miss_and_explicit_retry(cx: &mut TestAppContext) {
for fail_reads in [false, true] {
let test = KeyTest::new(&[], cx);
test.provider.state.lock().fail_reads = fail_reads;
test.load(URL, cx).await.expect("initial load");
{
let mut credentials = test.provider.state.lock();
credentials.fail_reads = false;
credentials
.keys
.insert(URL.to_string(), b"late-key".to_vec());
}
test.load(URL, cx).await.expect("cached miss");
assert_eq!(test.key(URL, cx), None);
test.provider.assert_reads(&[URL]);
let url = SharedString::from(URL);
let retry = test.state.update(cx, |state, cx| {
state.load_if_needed(url, |state| state, test.provider.clone(), cx)
});
retry.await.expect("explicit retry");
assert_eq!(test.key(URL, cx).as_deref(), Some("late-key"));
test.load(URL, cx).await.expect("cached key");
test.provider.assert_reads(&[URL, URL]);
}
}
#[gpui::test]
async fn test_disabled_gate_and_superseded_loads(cx: &mut TestAppContext) {
cx.update(|cx| {
set_credentials_read_gate::<DisabledAi>(|cx| cx.global::<DisabledAi>().0, cx);
});
let test = KeyTest::new(&[(OTHER_URL, "latest-key")], cx);
set_disabled_ai(true, cx);
let superseded = test.load(URL, cx);
cx.run_until_parked();
drop(test.load(OTHER_URL, cx));
cx.run_until_parked();
test.provider.assert_reads(&[]);
set_disabled_ai(false, cx);
set_disabled_ai(true, cx);
cx.run_until_parked();
test.provider.assert_reads(&[]);
let release = test.provider.defer_read(OTHER_URL);
set_disabled_ai(false, cx);
cx.run_until_parked();
drop(test.load(OTHER_URL, cx));
set_disabled_ai(true, cx);
cx.run_until_parked();
test.provider.assert_reads(&[OTHER_URL]);
release.send(()).expect("started read remains live");
cx.run_until_parked();
superseded.await.expect("superseded load completes");
assert_eq!(test.key(OTHER_URL, cx).as_deref(), Some("latest-key"));
test.provider.assert_reads(&[OTHER_URL]);
}
#[gpui::test]
async fn test_store_cancels_stale_read(cx: &mut TestAppContext) {
for stored_key in [Some("saved-key"), None] {
let test = KeyTest::new(&[(URL, "old-key")], cx);
let release = test.provider.defer_read(URL);
let load = test.load(URL, cx);
cx.run_until_parked();
test.store(stored_key, cx).await.expect("mutation");
cx.run_until_parked();
assert_eq!(release.send(()), Err(()));
load.await.expect("cancelled read completes");
test.load(URL, cx).await.expect("cached mutation");
assert_eq!(test.key(URL, cx).as_deref(), stored_key);
test.provider.assert_reads(&[URL]);
}
}
#[gpui::test]
async fn test_deferred_mutations_follow_url_identity(cx: &mut TestAppContext) {
for stored_key in [Some("saved-key"), None] {
let test = KeyTest::new(&[(URL, "old-key"), (OTHER_URL, "other-key")], cx);
test.load(URL, cx).await.expect("initial load");
let release = test.provider.defer_next_mutation();
let mutation = test.store(stored_key, cx);
cx.run_until_parked();
let release_read = test.provider.defer_read(OTHER_URL);
let load = test.load(OTHER_URL, cx);
cx.run_until_parked();
release.send(()).expect("pending mutation");
mutation.await.expect("mutation for inactive URL");
drop(test.load(OTHER_URL, cx));
cx.run_until_parked();
assert_eq!(test.key(OTHER_URL, cx), None);
release_read.send(()).expect("other URL read remains live");
load.await.expect("other URL load");
assert_eq!(test.key(OTHER_URL, cx).as_deref(), Some("other-key"));
assert_eq!(
test.provider.state.lock().keys.get(URL).map(Vec::as_slice),
stored_key.map(str::as_bytes)
);
test.provider.assert_reads(&[URL, OTHER_URL]);
}
}
type LoadTask = Task<Result<(), AuthenticateError>>;
struct DisabledAi(bool);
impl Global for DisabledAi {}
struct KeyTest {
state: Entity<ApiKeyState>,
provider: Arc<FakeCredentialsProvider>,
}
impl KeyTest {
fn new(keys: &[(&str, &str)], cx: &mut TestAppContext) -> Self {
let provider = Arc::new(FakeCredentialsProvider::default());
provider.state.lock().keys.extend(
keys.iter()
.map(|(url, key)| (url.to_string(), key.as_bytes().to_vec())),
);
let state = cx.new(|_| {
ApiKeyState::new(
SharedString::default(),
EnvVar {
name: SharedString::new_static("TEST_API_KEY"),
value: None,
},
)
});
Self { state, provider }
}
fn load(&self, url: &'static str, cx: &mut TestAppContext) -> LoadTask {
let url = SharedString::from(url);
self.state.update(cx, |state, cx| {
state.load_cached(url, |state| state, self.provider.clone(), cx)
})
}
fn store(&self, key: Option<&str>, cx: &mut TestAppContext) -> Task<Result<()>> {
let url = SharedString::from(URL);
let key = key.map(String::from);
self.state.update(cx, |state, cx| {
state.store(url, key, |state| state, self.provider.clone(), cx)
})
}
fn key(&self, url: &str, cx: &TestAppContext) -> Option<Arc<str>> {
self.state.read_with(cx, |state, _| state.key(url))
}
}
fn set_disabled_ai(disabled: bool, cx: &mut TestAppContext) {
cx.update(|cx| cx.set_global(DisabledAi(disabled)));
}
}
+2 -1
View File
@@ -17,7 +17,7 @@ use std::sync::Arc;
pub type CreateProviderSettingsView = Arc<dyn Fn(&mut Window, &mut App) -> AnyView + 'static>;
pub use crate::api_key::{ApiKey, ApiKeyState};
pub use crate::api_key::{ApiKey, ApiKeyState, read_credentials, set_credentials_read_gate};
pub use crate::registry::*;
pub use crate::request::{LanguageModelImageExt, gpui_size_to_image_size, image_size_to_gpui};
pub use env_var::{EnvVar, env_var};
@@ -559,6 +559,7 @@ pub trait LanguageModelProvider: LanguageModelClient {
fn is_authenticated(&self, cx: &App) -> bool;
fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>>;
fn refresh_models(&self, _cx: &mut App) {}
fn settings_view(&self, cx: &mut App) -> Option<ProviderSettingsView>;
fn set_api_key(&self, _key: Option<String>, _cx: &mut App) -> Task<Result<()>> {
+99 -3
View File
@@ -1,9 +1,10 @@
use crate::{
LanguageModel, LanguageModelCompletionError, LanguageModelId, LanguageModelProvider,
LanguageModelProviderId, LanguageModelProviderState, ZED_CLOUD_PROVIDER_ID, unavailable_error,
AuthenticateError, LanguageModel, LanguageModelCompletionError, LanguageModelId,
LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderState,
ZED_CLOUD_PROVIDER_ID, unavailable_error,
};
use collections::{BTreeMap, HashSet};
use gpui::{App, Context, Entity, EventEmitter, Global, prelude::*};
use gpui::{App, Context, Entity, EventEmitter, Global, Task, prelude::*};
use std::{str::FromStr, sync::Arc};
use thiserror::Error;
@@ -55,6 +56,7 @@ pub struct LanguageModelRegistry {
thread_summary_model: Option<LanguageModel>,
compaction_model: Option<LanguageModel>,
providers: BTreeMap<LanguageModelProviderId, Arc<dyn LanguageModelProvider>>,
authentication_tasks: BTreeMap<LanguageModelProviderId, Task<()>>,
inline_alternatives: Vec<LanguageModel>,
/// Set of installed extension IDs that provide language models.
/// Used to determine which built-in providers should be hidden.
@@ -164,16 +166,33 @@ impl LanguageModelRegistry {
subscription.detach();
}
let provider: Arc<dyn LanguageModelProvider> = provider;
if self
.providers
.get(&id)
.is_none_or(|registered| !Arc::ptr_eq(registered, &provider))
{
self.authentication_tasks.remove(&id);
}
self.providers.insert(id.clone(), provider);
cx.emit(Event::AddedProvider(id));
}
pub fn unregister_provider(&mut self, id: LanguageModelProviderId, cx: &mut Context<Self>) {
if self.providers.remove(&id).is_some() {
self.authentication_tasks.remove(&id);
cx.emit(Event::RemovedProvider(id));
}
}
pub fn authenticate_providers(&mut self, cx: &mut Context<Self>) {
self.authenticate_providers_with_retry(false, cx);
}
pub fn retry_authentication(&mut self, cx: &mut Context<Self>) {
self.authenticate_providers_with_retry(true, cx);
}
pub fn providers(&self) -> Vec<Arc<dyn LanguageModelProvider>> {
let zed_provider_id = LanguageModelProviderId("zed.dev".into());
let mut providers = Vec::with_capacity(self.providers.len());
@@ -522,6 +541,83 @@ impl LanguageModelRegistry {
pub fn inline_alternative_models(&self) -> &[LanguageModel] {
&self.inline_alternatives
}
fn authenticate_providers_with_retry(&mut self, retry: bool, cx: &mut Context<Self>) {
for provider in self.visible_providers() {
let provider_id = provider.id();
if let Some(task) = self.authentication_tasks.get(&provider_id) {
if !retry {
let registry = cx.weak_entity();
cx.defer(move |cx| {
if registry
.read_with(cx, |registry, _| {
registry
.providers
.get(&provider_id)
.is_some_and(|registered| Arc::ptr_eq(registered, &provider))
})
.unwrap_or(false)
{
provider.refresh_models(cx);
}
});
continue;
}
if !task.is_ready() {
continue;
}
}
if retry && provider.is_authenticated(cx) {
continue;
}
self.authentication_tasks.insert(
provider_id.clone(),
cx.spawn(async move |registry, cx| {
if let Err(error) = cx.update(|cx| provider.authenticate(cx)).await {
match error {
AuthenticateError::CredentialsNotFound => {
// Since we're authenticating these providers in the
// background for the purposes of populating the
// language selector, we don't care about providers
// where the credentials are not found.
}
AuthenticateError::ConnectionRefused => {
// Not logging connection refused errors as they are mostly from LM Studio's noisy auth failures.
// LM Studio only has one auth method (endpoint call) which fails for users who haven't enabled it.
// TODO: Better manage LM Studio auth logic to avoid these noisy failures.
}
_ => {
// Some providers have noisy failure states that we
// don't want to spam the logs with every time the
// language model selector is initialized.
//
// Ideally these should have more clear failure modes
// that we know are safe to ignore here, like what we do
// with `CredentialsNotFound` above.
match provider_id.0.as_ref() {
"lmstudio" | "ollama" => {
// LM Studio and Ollama both make fetch requests to the local APIs to determine if they are "authenticated".
//
// These fail noisily, so we don't log them.
}
"copilot_chat" => {
// Copilot Chat returns an error if Copilot is not enabled, so we don't log those errors.
}
_ => log::error!(
"Failed to authenticate provider: {}: {error:#}",
provider.name().0
),
}
}
}
}
registry
.update(cx, |registry, cx| registry.refresh_fallback_model(cx))
.ok();
}),
);
}
}
}
#[cfg(test)]
+1
View File
@@ -74,6 +74,7 @@ x_ai_subscribed.workspace = true
client = { workspace = true, features = ["test-support"] }
clock = { workspace = true, features = ["test-support"] }
cloud_llm_client.workspace = true
credentials_provider = { workspace = true, features = ["test-support"] }
db = { workspace = true, features = ["test-support"] }
feature_flags.workspace = true
gpui = { workspace = true, features = ["test-support"] }
@@ -35,6 +35,7 @@ use crate::provider::x_ai_subscribed::XAiSubscribedProvider;
pub use crate::settings::*;
pub fn init(user_store: Entity<UserStore>, client: Arc<Client>, cx: &mut App) {
language_model::set_credentials_read_gate::<SettingsStore>(ai_disabled, cx);
let credentials_provider = client.credentials_provider();
let registry = LanguageModelRegistry::global(cx);
registry.update(cx, |registry, cx| {
@@ -139,6 +140,16 @@ pub fn init(user_store: Entity<UserStore>, client: Arc<Client>, cx: &mut App) {
.detach();
}
fn ai_disabled(cx: &App) -> bool {
cx.try_global::<SettingsStore>().is_some_and(|store| {
store
.merged_settings()
.project
.disable_ai
.is_some_and(|disabled| disabled.0)
})
}
#[derive(Default, PartialEq, Eq)]
struct CompatibleProviders(HashMap<Arc<str>, CompatibleProviderKind>);
@@ -350,6 +350,14 @@ impl LanguageModelProvider for AnthropicLanguageModelProvider {
self.state.update(cx, |state, cx| state.authenticate(cx))
}
fn refresh_models(&self, cx: &mut App) {
self.state.update(cx, |state, cx| {
if state.is_authenticated() {
state.restart_fetch_models_task(cx);
}
});
}
fn settings_view(&self, cx: &mut App) -> Option<ProviderSettingsView> {
let state = self.state.read(cx);
Some(ProviderSettingsView::ApiKey(ApiKeyConfiguration::new(
@@ -564,10 +564,10 @@ impl State {
}
// Try keychain
let (_, credentials_bytes) = credentials_provider
.read_credentials(AMAZON_AWS_URL, cx)
.await?
.ok_or(AuthenticateError::CredentialsNotFound)?;
let (_, credentials_bytes) =
language_model::read_credentials(credentials_provider.as_ref(), AMAZON_AWS_URL, cx)
.await?
.ok_or(AuthenticateError::CredentialsNotFound)?;
let credentials_str = String::from_utf8(credentials_bytes)
.with_context(|| format!("invalid {PROVIDER_NAME} credentials"))?;
@@ -339,6 +339,11 @@ impl LanguageModelProvider for GoogleLanguageModelProvider {
self.state.update(cx, |state, cx| state.authenticate(cx))
}
fn refresh_models(&self, cx: &mut App) {
self.state
.update(cx, |state, cx| state.restart_fetch_models_task(cx));
}
fn settings_view(&self, cx: &mut App) -> Option<ProviderSettingsView> {
let state = self.state.read(cx);
Some(ProviderSettingsView::ApiKey(ApiKeyConfiguration::new(
@@ -691,6 +691,11 @@ impl LanguageModelProvider for LlamaCppLanguageModelProvider {
self.state.update(cx, |state, cx| state.authenticate(cx))
}
fn refresh_models(&self, cx: &mut App) {
self.state
.update(cx, |state, cx| state.restart_fetch_models_task(cx));
}
fn settings_view(&self, _cx: &mut App) -> Option<ProviderSettingsView> {
let state = self.state.clone();
Some(ProviderSettingsView::SubPage(
@@ -1470,47 +1475,13 @@ impl Render for ConfigurationView {
#[cfg(test)]
mod tests {
use super::*;
use credentials_provider::test_support::FakeCredentialsProvider;
use gpui::TestAppContext;
use http_client::FakeHttpClient;
use language_model::LanguageModelToolUse;
use language_model::{LanguageModelRegistry, LanguageModelToolUse};
use parking_lot::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};
struct FakeCredentialsProvider {
api_key: Vec<u8>,
}
impl CredentialsProvider for FakeCredentialsProvider {
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>,
> {
let api_key = self.api_key.clone();
Box::pin(async move { Ok(Some(("Bearer".to_string(), api_key))) })
}
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(()) })
}
}
fn entry(id: &str, n_ctx: Option<u64>, n_ctx_train: Option<u64>) -> ModelEntry {
ModelEntry {
id: id.to_string(),
@@ -1816,9 +1787,7 @@ mod tests {
let provider = cx.update(|cx| {
LlamaCppLanguageModelProvider::new(
FakeHttpClient::with_404_response(),
Arc::new(FakeCredentialsProvider {
api_key: Vec::new(),
}),
Arc::new(FakeCredentialsProvider::default()),
cx,
)
});
@@ -1881,6 +1850,79 @@ mod tests {
));
}
#[gpui::test]
fn registry_refreshes_models_while_credentials_are_pending(cx: &mut TestAppContext) {
cx.update(|cx| {
let settings_store = SettingsStore::test(cx);
cx.set_global(settings_store);
});
let request_count = AtomicUsize::new(0);
let http_client = FakeHttpClient::create(move |request| {
let first_request = request_count.fetch_add(1, Ordering::SeqCst) == 0;
async move {
let (status, body) = match request.uri().path() {
"/v1/models" if first_request => (503, "not ready"),
"/v1/models" => (200, r#"{"data":[{"id":"refreshed-model"}]}"#),
"/props" => (200, "{}"),
path => anyhow::bail!("unexpected request: {path}"),
};
Ok(http_client::Response::builder()
.status(status)
.body(http_client::AsyncBody::from(body))?)
}
});
let credentials = Arc::new(FakeCredentialsProvider::default());
let pending_credentials = credentials.defer_read(LLAMA_CPP_API_URL);
let provider = cx.update(|cx| {
let provider = Arc::new(LlamaCppLanguageModelProvider::new(
http_client,
credentials.clone(),
cx,
));
provider.state.update(cx, |state, cx| {
state.api_key_state = ApiKeyState::new(
LlamaCppLanguageModelProvider::api_url(cx),
EnvVar {
name: SharedString::from(API_KEY_ENV_VAR_NAME),
value: None,
},
);
});
provider
});
cx.run_until_parked();
let registry = cx.new(|_| LanguageModelRegistry::default());
registry.update(cx, |registry, cx| {
registry.register_provider(provider.clone(), cx);
registry.authenticate_providers(cx);
});
cx.run_until_parked();
assert!(cx.update(|cx| provider.provided_models(cx).is_empty()));
credentials.assert_reads(&[LLAMA_CPP_API_URL]);
registry.update(cx, |registry, cx| registry.authenticate_providers(cx));
cx.run_until_parked();
assert_eq!(
cx.update(|cx| provider
.provided_models(cx)
.into_iter()
.map(|model| model.id.0.to_string())
.collect::<Vec<_>>()),
["refreshed-model"]
);
credentials.assert_reads(&[LLAMA_CPP_API_URL]);
assert!(!pending_credentials.is_canceled());
let provider_id = provider.id();
drop(provider);
registry.update(cx, |registry, cx| {
registry.unregister_provider(provider_id, cx)
});
cx.run_until_parked();
cx.update(|_| ());
cx.run_until_parked();
assert!(pending_credentials.is_canceled());
}
#[gpui::test]
async fn authenticate_fetches_models_after_loading_api_key(cx: &mut TestAppContext) {
cx.update(|cx| {
@@ -1932,9 +1974,12 @@ mod tests {
}
}
});
let credentials_provider = Arc::new(FakeCredentialsProvider {
api_key: b"loaded-key".to_vec(),
});
let credentials_provider = Arc::new(FakeCredentialsProvider::default());
credentials_provider
.state
.lock()
.keys
.insert(LLAMA_CPP_API_URL.to_string(), b"loaded-key".to_vec());
let provider = cx
.update(|cx| LlamaCppLanguageModelProvider::new(http_client, credentials_provider, cx));
@@ -1963,9 +2008,7 @@ mod tests {
.status(500)
.body(http_client::AsyncBody::from("server error"))?)
});
let credentials_provider = Arc::new(FakeCredentialsProvider {
api_key: b"test-key".to_vec(),
});
let credentials_provider = Arc::new(FakeCredentialsProvider::default());
let provider = cx
.update(|cx| LlamaCppLanguageModelProvider::new(http_client, credentials_provider, cx));
cx.run_until_parked();
+78 -35
View File
@@ -140,7 +140,7 @@ impl State {
fn authenticate(&mut self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {
let credentials_provider = self.credentials_provider.clone();
let api_url = LmStudioLanguageModelProvider::api_url(cx).into();
let _task = self.api_key_state.load_if_needed(
let load_key = self.api_key_state.load_if_needed(
api_url,
|this| &mut this.api_key_state,
credentials_provider,
@@ -151,8 +151,9 @@ impl State {
return Task::ready(Ok(()));
}
let fetch_models_task = self.fetch_models(cx);
cx.spawn(async move |_this, _cx| {
cx.spawn(async move |state, cx| {
load_key.await?;
let fetch_models_task = state.update(cx, |state, cx| state.fetch_models(cx))?;
match fetch_models_task.await {
Ok(()) => Ok(()),
Err(err) => {
@@ -329,6 +330,11 @@ impl LanguageModelProvider for LmStudioLanguageModelProvider {
self.state.update(cx, |state, cx| state.authenticate(cx))
}
fn refresh_models(&self, cx: &mut App) {
self.state
.update(cx, |state, cx| state.restart_fetch_models_task(cx));
}
fn settings_view(&self, _cx: &mut App) -> Option<ProviderSettingsView> {
let state = self.state.clone();
Some(ProviderSettingsView::SubPage(
@@ -911,39 +917,72 @@ impl Render for ConfigurationView {
#[cfg(test)]
mod tests {
use super::*;
use credentials_provider::test_support::FakeCredentialsProvider;
use gpui::TestAppContext;
use http_client::FakeHttpClient;
use http_client::{AsyncBody, FakeHttpClient, Response};
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 enabling_ai_discovers_models_with_stored_credentials(cx: &mut TestAppContext) {
cx.update(|cx| {
let mut store = SettingsStore::test(cx);
store.update_user_settings(cx, |settings| {
settings.project.disable_ai = Some(true.into());
});
cx.set_global(store);
language_model::set_credentials_read_gate::<SettingsStore>(crate::ai_disabled, cx);
});
let credentials = Arc::new(FakeCredentialsProvider::default());
credentials
.state
.lock()
.keys
.insert(LMSTUDIO_API_URL.to_string(), b"stored-key".to_vec());
let client = FakeHttpClient::create(|request| async move {
if request
.headers()
.get("Authorization")
.and_then(|value| value.to_str().ok())
!= Some("Bearer stored-key")
{
return Ok(Response::builder().status(401).body(AsyncBody::default())?);
}
Ok(Response::builder().status(200).body(AsyncBody::from(
r#"{"data":[{"id":"stored-model","object":"model","type":"llm","publisher":"test","compatibility_type":"gguf","state":"loaded"}]}"#,
))?)
});
let provider = cx.update(|cx| {
let provider = LmStudioLanguageModelProvider::new(client, credentials.clone(), cx);
provider.state.update(cx, |state, _| {
state.api_key_state = ApiKeyState::new(
SharedString::from(LMSTUDIO_API_URL),
EnvVar {
name: SharedString::from("TEST_API_KEY"),
value: None,
},
);
});
provider
});
let authentication = cx.update(|cx| provider.authenticate(cx));
cx.run_until_parked();
credentials.assert_reads(&[]);
cx.update_global::<SettingsStore, _>(|store, cx| {
store.update_user_settings(cx, |settings| {
settings.project.disable_ai = Some(false.into());
});
});
authentication
.await
.expect("authentication after enabling AI");
assert_eq!(
cx.update(|cx| provider
.provided_models(cx)
.into_iter()
.map(|model| model.id.0.to_string())
.collect::<Vec<_>>()),
["stored-model"]
);
credentials.assert_reads(&[LMSTUDIO_API_URL]);
}
#[gpui::test]
@@ -959,7 +998,11 @@ mod tests {
.body(http_client::AsyncBody::from("server error"))?)
});
let provider = cx.update(|cx| {
LmStudioLanguageModelProvider::new(http_client, Arc::new(TestCredentialsProvider), cx)
LmStudioLanguageModelProvider::new(
http_client,
Arc::new(FakeCredentialsProvider::default()),
cx,
)
});
cx.run_until_parked();
@@ -501,6 +501,11 @@ impl LanguageModelProvider for OllamaLanguageModelProvider {
self.state.update(cx, |state, cx| state.authenticate(cx))
}
fn refresh_models(&self, cx: &mut App) {
self.state
.update(cx, |state, cx| state.restart_fetch_models_task(cx));
}
fn settings_view(&self, _cx: &mut App) -> Option<ProviderSettingsView> {
let state = self.state.clone();
Some(ProviderSettingsView::SubPage(
@@ -350,6 +350,11 @@ impl LanguageModelProvider for OpenRouterLanguageModelProvider {
self.state.update(cx, |state, cx| state.authenticate(cx))
}
fn refresh_models(&self, cx: &mut App) {
self.state
.update(cx, |state, cx| state.restart_fetch_models_task(cx));
}
fn settings_view(&self, cx: &mut App) -> Option<ProviderSettingsView> {
let state = self.state.read(cx);
Some(ProviderSettingsView::ApiKey(ApiKeyConfiguration::new(
@@ -528,6 +528,11 @@ impl LanguageModelProvider for OpenCodeLanguageModelProvider {
self.state.update(cx, |state, cx| state.authenticate(cx))
}
fn refresh_models(&self, cx: &mut App) {
self.state
.update(cx, |state, cx| state.restart_fetch_models_task(cx));
}
fn settings_view(&self, _cx: &mut App) -> Option<ProviderSettingsView> {
let state = self.state.clone();
Some(ProviderSettingsView::SubPage(
@@ -313,6 +313,11 @@ impl LanguageModelProvider for VercelAiGatewayLanguageModelProvider {
self.state.update(cx, |state, cx| state.authenticate(cx))
}
fn refresh_models(&self, cx: &mut App) {
self.state
.update(cx, |state, cx| state.restart_fetch_models_task(cx));
}
fn settings_view(&self, cx: &mut App) -> Option<ProviderSettingsView> {
let state = self.state.read(cx);
Some(ProviderSettingsView::ApiKey(ApiKeyConfiguration::new(
@@ -120,10 +120,16 @@ impl State {
.spawn({
let credentials_provider = credentials_provider.clone();
async move |this, cx| {
let result = credentials_provider
.read_credentials(CREDENTIALS_KEY, cx)
.await;
let result = language_model::read_credentials(
credentials_provider.as_ref(),
CREDENTIALS_KEY,
cx,
)
.await;
let refresh_models_task = this.update(cx, |state, cx| {
if state.auth_generation != 0 {
return None;
}
match result {
Ok(Some((_, bytes))) => {
match serde_json::from_slice::<CodexCredentials>(&bytes) {
@@ -1715,7 +1721,7 @@ mod tests {
))?)
});
let state = cx.new(|cx| State::new(http, creds_provider, cx));
let state = cx.new(|cx| State::new(http.clone(), creds_provider.clone(), cx));
let load_task = cx
.read(|cx| state.read(cx).load_task())
@@ -1733,6 +1739,25 @@ mod tests {
Some("gpt-account-default")
);
});
let state = cx.new(|cx| State::new(http, creds_provider, cx));
state.update(cx, |state, _| state.auth_generation += 1);
let load_task = cx
.read(|cx| state.read(cx).load_task())
.expect("constructor should start the credentials load");
cx.run_until_parked();
load_task.await.expect("stale load should complete");
cx.read(|cx| {
let state = state.read(cx);
assert_eq!(
state
.credentials
.as_ref()
.map(|creds| creds.access_token.as_str()),
None
);
assert!(state.load_task().is_none());
});
}
#[gpui::test]
+1
View File
@@ -76,6 +76,7 @@ zed_actions.workspace = true
zed_credentials_provider.workspace = true
[dev-dependencies]
credentials_provider = { workspace = true, features = ["test-support"] }
fs = { workspace = true, features = ["test-support"] }
futures.workspace = true
gpui = { workspace = true, features = ["test-support"] }
+87 -1
View File
@@ -18,7 +18,7 @@ use gpui::{
use language::Buffer;
use platform_title_bar::PlatformTitleBar;
use project::{Project, ProjectPath, Worktree, WorktreeId};
use project::{DisableAiSettings, Project, ProjectPath, Worktree, WorktreeId};
use release_channel::ReleaseChannel;
use schemars::JsonSchema;
use serde::Deserialize;
@@ -4349,6 +4349,7 @@ impl SettingsWindow {
self.sandbox_host_validation_error = None;
self.sub_page_stack
.push(SubPage::new(sub_page_link, section_header));
self.retry_llm_provider_authentication(cx);
self.content_focus_handle.focus_handle(cx).focus(window, cx);
cx.notify();
}
@@ -4559,10 +4560,22 @@ impl SettingsWindow {
{
self.skill_creator_page = None;
}
self.retry_llm_provider_authentication(cx);
self.content_focus_handle.focus_handle(cx).focus(window, cx);
cx.notify();
}
fn retry_llm_provider_authentication(&self, cx: &mut Context<Self>) {
if self.sub_page_stack.last().is_some_and(|sub_page| {
sub_page.link.json_path == Some("llm_providers")
&& sub_page.link.title.as_ref() == "LLM Providers"
}) && !DisableAiSettings::get_global(cx).disable_ai
{
language_model::LanguageModelRegistry::global(cx)
.update(cx, |registry, cx| registry.retry_authentication(cx));
}
}
pub(crate) fn active_project(&self, cx: &App) -> Option<Entity<Project>> {
let original_window = self.original_window.as_ref()?;
let multi_workspace = original_window.read(cx).ok()?;
@@ -5427,6 +5440,9 @@ fn render_icon_theme_picker(
pub mod test {
use super::*;
use credentials_provider::test_support::FakeCredentialsProvider;
use language_model::{ApiKeyState, EnvVar, LanguageModelProviderState, LanguageModelRegistry};
use language_models::provider::open_ai_compatible::OpenAiCompatibleLanguageModelProvider;
impl SettingsWindow {
fn navbar_entry(&self) -> usize {
@@ -7286,6 +7302,76 @@ pub mod test {
.unwrap();
}
#[gpui::test]
fn test_llm_provider_authentication_and_settings_retry(cx: &mut gpui::TestAppContext) {
let url = "https://settings-provider.example/v1";
let credentials = Arc::new(FakeCredentialsProvider::default());
let release = credentials.defer_read(url);
let provider = cx.update(|cx| {
register_settings(cx);
let app_state = AppState::test(cx);
AppState::set_global(app_state.clone(), cx);
let settings = r#"{"language_models":{"openai_compatible":{
"settings-retry":{"api_url":"https://settings-provider.example/v1",
"available_models":[]}
}}}"#;
cx.update_global::<SettingsStore, _>(|store, cx| {
store
.set_user_settings(settings, cx)
.expect("provider settings");
});
let provider = Arc::new(OpenAiCompatibleLanguageModelProvider::new(
Arc::from("settings-retry"),
app_state.client.http_client(),
credentials.clone(),
cx,
));
let env_var = EnvVar {
name: SharedString::from("TEST_SETTINGS_API_KEY"),
value: None,
};
let state = provider.observable_entity().expect("provider state");
state.update(cx, |state, _| {
state.api_key_state = ApiKeyState::new(SharedString::from(url), env_var);
});
LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
registry.register_provider(provider.clone(), cx);
registry.authenticate_providers(cx);
});
provider
});
let (settings_window, cx) =
cx.add_window_view(|window, cx| SettingsWindow::new(None, window, cx));
cx.run_until_parked();
for release in [None, Some(release)] {
if let Some(release) = release {
release.send(()).expect("authentication remains live");
cx.run_until_parked();
}
cx.cx.update(|cx| {
LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
registry.register_provider(provider.clone(), cx);
registry.authenticate_providers(cx);
registry.authenticate_providers(cx);
});
});
cx.run_until_parked();
credentials.assert_reads(&[url]);
}
cx.cx
.update(|cx| open_settings_editor(Some("llm_providers"), None, None, cx));
cx.run_until_parked();
credentials.assert_reads(&[url; 2]);
settings_window.update(cx, |_, cx| cx.notify());
cx.draw(
point(px(0.), px(0.)),
gpui::size(px(1000.), px(700.)),
|_, _| settings_window.clone().into_any_element(),
);
cx.run_until_parked();
credentials.assert_reads(&[url; 2]);
}
fn settings_window_with_items(
items: Vec<SettingsPageItem>,
window: &mut Window,
+31 -6
View File
@@ -92,10 +92,18 @@ impl State {
.spawn({
let credentials_provider = credentials_provider.clone();
async move |this, cx| {
let result = credentials_provider
.read_credentials(CREDENTIALS_KEY, cx)
.await;
let result = language_model::read_credentials(
credentials_provider.as_ref(),
CREDENTIALS_KEY,
cx,
)
.await;
this.update(cx, |state, cx| {
state.load_task = None;
cx.notify();
if state.auth_generation != 0 {
return;
}
match result {
Ok(Some((_, bytes))) => {
match serde_json::from_slice::<SuperGrokCredentials>(&bytes) {
@@ -116,8 +124,6 @@ impl State {
log::error!("Failed to load SuperGrok credentials: {error:#}");
}
}
state.load_task = None;
cx.notify();
})?;
Ok::<(), Arc<anyhow::Error>>(())
}
@@ -1321,7 +1327,7 @@ mod tests {
.status(200)
.body(http_client::AsyncBody::default())?)
});
let state = cx.new(|cx| State::new(http, creds_provider, cx));
let state = cx.new(|cx| State::new(http.clone(), creds_provider.clone(), cx));
let load_task = cx
.read(|cx| state.read(cx).load_task())
.expect("constructor should start the credentials load");
@@ -1334,6 +1340,25 @@ mod tests {
assert!(state.is_authenticated());
assert!(state.load_task().is_none());
});
let state = cx.new(|cx| State::new(http, creds_provider, cx));
state.update(cx, |state, _| state.auth_generation += 1);
let load_task = cx
.read(|cx| state.read(cx).load_task())
.expect("constructor should start the credentials load");
cx.run_until_parked();
load_task.await.expect("stale load should complete");
cx.read(|cx| {
let state = state.read(cx);
assert_eq!(
state
.credentials
.as_ref()
.map(|creds| creds.access_token.as_str()),
None
);
assert!(state.load_task().is_none());
});
}
struct FakeCredentialsProvider {