assistant panel: Fix panel not reloading after entering credentials (#15531)

This is the revised version of #15527.

We also added new events to notify subscribers when new providers are
added or removed.

Co-Authored-by: Thorsten <thorsten@zed.dev>

Release Notes:

- N/A

---------

Co-authored-by: Thorsten <thorsten@zed.dev>
Co-authored-by: Thorsten Ball <mrnugget@gmail.com>
This commit is contained in:
Bennet Bo Fenner 2024-07-31 14:12:17 +02:00 committed by GitHub
parent a31dba9fc1
commit 821ce2fc7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 119 additions and 68 deletions

View file

@ -394,8 +394,15 @@ impl AssistantPanel {
cx.subscribe(&context_store, Self::handle_context_store_event),
cx.subscribe(
&LanguageModelRegistry::global(cx),
|this, _, _: &language_model::ActiveModelChanged, cx| {
this.completion_provider_changed(cx);
|this, _, event: &language_model::Event, cx| match event {
language_model::Event::ActiveModelChanged => {
this.completion_provider_changed(cx);
}
language_model::Event::ProviderStateChanged
| language_model::Event::AddedProvider(_)
| language_model::Event::RemovedProvider(_) => {
this.ensure_authenticated(cx);
}
},
),
];
@ -588,6 +595,11 @@ impl AssistantPanel {
}
fn ensure_authenticated(&mut self, cx: &mut ViewContext<Self>) {
if self.is_authenticated(cx) {
self.set_authentication_prompt(None, cx);
return;
}
let Some(provider_id) = LanguageModelRegistry::read_global(cx)
.active_provider()
.map(|p| p.id())
@ -596,29 +608,35 @@ impl AssistantPanel {
};
let load_credentials = self.authenticate(cx);
let task = cx.spawn(|this, mut cx| async move {
let _ = load_credentials.await;
this.update(&mut cx, |this, cx| {
this.show_authentication_prompt(cx);
})
.log_err();
});
self.authenticate_provider_task = Some((provider_id, task));
self.authenticate_provider_task = Some((
provider_id,
cx.spawn(|this, mut cx| async move {
let _ = load_credentials.await;
this.update(&mut cx, |this, cx| {
this.show_authentication_prompt(cx);
this.authenticate_provider_task = None;
})
.log_err();
}),
));
}
fn show_authentication_prompt(&mut self, cx: &mut ViewContext<Self>) {
let prompt = Self::authentication_prompt(cx);
self.set_authentication_prompt(prompt, cx);
}
fn set_authentication_prompt(&mut self, prompt: Option<AnyView>, cx: &mut ViewContext<Self>) {
if self.active_context_editor(cx).is_none() {
self.new_context(cx);
}
let authentication_prompt = Self::authentication_prompt(cx);
for context_editor in self.context_editors(cx) {
context_editor.update(cx, |editor, cx| {
editor.set_authentication_prompt(authentication_prompt.clone(), cx);
editor.set_authentication_prompt(prompt.clone(), cx);
});
}
cx.notify();
}