Revert "assistant panel: Fix entering credentials not updating view" (#15528)

Reverts zed-industries/zed#15527

We broke the assistant panel in the process...

Release Notes:

- N/A
This commit is contained in:
Bennet Bo Fenner 2024-07-31 13:26:27 +02:00 committed by GitHub
parent b571bc800d
commit 380a19fcf2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 52 additions and 92 deletions

View file

@ -393,13 +393,8 @@ impl AssistantPanel {
cx.subscribe(&context_store, Self::handle_context_store_event), cx.subscribe(&context_store, Self::handle_context_store_event),
cx.subscribe( cx.subscribe(
&LanguageModelRegistry::global(cx), &LanguageModelRegistry::global(cx),
|this, _, event: &language_model::Event, cx| match event { |this, _, _: &language_model::ActiveModelChanged, cx| {
language_model::Event::ActiveModelChanged => { this.completion_provider_changed(cx);
this.completion_provider_changed(cx);
}
language_model::Event::ProviderStateChanged => {
this.ensure_authenticated(cx);
}
}, },
), ),
]; ];
@ -592,16 +587,6 @@ impl AssistantPanel {
} }
fn ensure_authenticated(&mut self, cx: &mut ViewContext<Self>) { fn ensure_authenticated(&mut self, cx: &mut ViewContext<Self>) {
if self.is_authenticated(cx) {
for context_editor in self.context_editors(cx) {
context_editor.update(cx, |editor, cx| {
editor.set_authentication_prompt(None, cx);
});
}
cx.notify();
return;
}
let Some(provider_id) = LanguageModelRegistry::read_global(cx) let Some(provider_id) = LanguageModelRegistry::read_global(cx)
.active_provider() .active_provider()
.map(|p| p.id()) .map(|p| p.id())
@ -610,18 +595,15 @@ impl AssistantPanel {
}; };
let load_credentials = self.authenticate(cx); 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(( self.authenticate_provider_task = Some((provider_id, task));
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>) { fn show_authentication_prompt(&mut self, cx: &mut ViewContext<Self>) {

View file

@ -86,25 +86,10 @@ pub trait LanguageModelProvider: 'static {
fn authenticate(&self, cx: &mut AppContext) -> Task<Result<()>>; fn authenticate(&self, cx: &mut AppContext) -> Task<Result<()>>;
fn authentication_prompt(&self, cx: &mut WindowContext) -> AnyView; fn authentication_prompt(&self, cx: &mut WindowContext) -> AnyView;
fn reset_credentials(&self, cx: &mut AppContext) -> Task<Result<()>>; fn reset_credentials(&self, cx: &mut AppContext) -> Task<Result<()>>;
// fn observable_entity(&self) ;
} }
pub trait LanguageModelProviderState: 'static { pub trait LanguageModelProviderState: 'static {
type ObservableEntity; fn subscribe<T: 'static>(&self, cx: &mut gpui::ModelContext<T>) -> Option<gpui::Subscription>;
fn observable_entity(&self) -> Option<gpui::Model<Self::ObservableEntity>>;
fn subscribe<T: 'static>(
&self,
cx: &mut gpui::ModelContext<T>,
callback: impl Fn(&mut T, &mut gpui::ModelContext<T>) + 'static,
) -> Option<gpui::Subscription> {
let entity = self.observable_entity()?;
Some(cx.observe(&entity, move |this, _, cx| {
callback(this, cx);
}))
}
} }
#[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)] #[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]

View file

@ -44,7 +44,7 @@ pub struct AnthropicLanguageModelProvider {
state: gpui::Model<State>, state: gpui::Model<State>,
} }
pub struct State { struct State {
api_key: Option<String>, api_key: Option<String>,
_subscription: Subscription, _subscription: Subscription,
} }
@ -61,12 +61,11 @@ impl AnthropicLanguageModelProvider {
Self { http_client, state } Self { http_client, state }
} }
} }
impl LanguageModelProviderState for AnthropicLanguageModelProvider { impl LanguageModelProviderState for AnthropicLanguageModelProvider {
type ObservableEntity = State; fn subscribe<T: 'static>(&self, cx: &mut gpui::ModelContext<T>) -> Option<gpui::Subscription> {
Some(cx.observe(&self.state, |_, _, cx| {
fn observable_entity(&self) -> Option<gpui::Model<Self::ObservableEntity>> { cx.notify();
Some(self.state.clone()) }))
} }
} }

View file

@ -50,7 +50,7 @@ pub struct CloudLanguageModelProvider {
_maintain_client_status: Task<()>, _maintain_client_status: Task<()>,
} }
pub struct State { struct State {
client: Arc<Client>, client: Arc<Client>,
status: client::Status, status: client::Status,
_subscription: Subscription, _subscription: Subscription,
@ -99,10 +99,10 @@ impl CloudLanguageModelProvider {
} }
impl LanguageModelProviderState for CloudLanguageModelProvider { impl LanguageModelProviderState for CloudLanguageModelProvider {
type ObservableEntity = State; fn subscribe<T: 'static>(&self, cx: &mut gpui::ModelContext<T>) -> Option<gpui::Subscription> {
Some(cx.observe(&self.state, |_, _, cx| {
fn observable_entity(&self) -> Option<gpui::Model<Self::ObservableEntity>> { cx.notify();
Some(self.state.clone()) }))
} }
} }

View file

@ -11,8 +11,8 @@ use futures::future::BoxFuture;
use futures::stream::BoxStream; use futures::stream::BoxStream;
use futures::{FutureExt, StreamExt}; use futures::{FutureExt, StreamExt};
use gpui::{ use gpui::{
percentage, svg, Animation, AnimationExt, AnyView, AppContext, AsyncAppContext, Model, Render, percentage, svg, Animation, AnimationExt, AnyView, AppContext, AsyncAppContext, Model,
Subscription, Task, Transformation, ModelContext, Render, Subscription, Task, Transformation,
}; };
use settings::{Settings, SettingsStore}; use settings::{Settings, SettingsStore};
use std::time::Duration; use std::time::Duration;
@ -67,10 +67,10 @@ impl CopilotChatLanguageModelProvider {
} }
impl LanguageModelProviderState for CopilotChatLanguageModelProvider { impl LanguageModelProviderState for CopilotChatLanguageModelProvider {
type ObservableEntity = State; fn subscribe<T: 'static>(&self, cx: &mut ModelContext<T>) -> Option<Subscription> {
Some(cx.observe(&self.state, |_, _, cx| {
fn observable_entity(&self) -> Option<gpui::Model<Self::ObservableEntity>> { cx.notify();
Some(self.state.clone()) }))
} }
} }

View file

@ -36,9 +36,7 @@ pub struct FakeLanguageModelProvider {
} }
impl LanguageModelProviderState for FakeLanguageModelProvider { impl LanguageModelProviderState for FakeLanguageModelProvider {
type ObservableEntity = (); fn subscribe<T: 'static>(&self, _: &mut gpui::ModelContext<T>) -> Option<gpui::Subscription> {
fn observable_entity(&self) -> Option<gpui::Model<Self::ObservableEntity>> {
None None
} }
} }

View file

@ -44,7 +44,7 @@ pub struct GoogleLanguageModelProvider {
state: gpui::Model<State>, state: gpui::Model<State>,
} }
pub struct State { struct State {
api_key: Option<String>, api_key: Option<String>,
_subscription: Subscription, _subscription: Subscription,
} }
@ -63,10 +63,10 @@ impl GoogleLanguageModelProvider {
} }
impl LanguageModelProviderState for GoogleLanguageModelProvider { impl LanguageModelProviderState for GoogleLanguageModelProvider {
type ObservableEntity = State; fn subscribe<T: 'static>(&self, cx: &mut gpui::ModelContext<T>) -> Option<gpui::Subscription> {
Some(cx.observe(&self.state, |_, _, cx| {
fn observable_entity(&self) -> Option<gpui::Model<Self::ObservableEntity>> { cx.notify();
Some(self.state.clone()) }))
} }
} }

View file

@ -32,7 +32,7 @@ pub struct OllamaLanguageModelProvider {
state: gpui::Model<State>, state: gpui::Model<State>,
} }
pub struct State { struct State {
http_client: Arc<dyn HttpClient>, http_client: Arc<dyn HttpClient>,
available_models: Vec<ollama::Model>, available_models: Vec<ollama::Model>,
_subscription: Subscription, _subscription: Subscription,
@ -87,10 +87,10 @@ impl OllamaLanguageModelProvider {
} }
impl LanguageModelProviderState for OllamaLanguageModelProvider { impl LanguageModelProviderState for OllamaLanguageModelProvider {
type ObservableEntity = State; fn subscribe<T: 'static>(&self, cx: &mut gpui::ModelContext<T>) -> Option<gpui::Subscription> {
Some(cx.observe(&self.state, |_, _, cx| {
fn observable_entity(&self) -> Option<gpui::Model<Self::ObservableEntity>> { cx.notify();
Some(self.state.clone()) }))
} }
} }

View file

@ -45,7 +45,7 @@ pub struct OpenAiLanguageModelProvider {
state: gpui::Model<State>, state: gpui::Model<State>,
} }
pub struct State { struct State {
api_key: Option<String>, api_key: Option<String>,
_subscription: Subscription, _subscription: Subscription,
} }
@ -64,10 +64,10 @@ impl OpenAiLanguageModelProvider {
} }
impl LanguageModelProviderState for OpenAiLanguageModelProvider { impl LanguageModelProviderState for OpenAiLanguageModelProvider {
type ObservableEntity = State; fn subscribe<T: 'static>(&self, cx: &mut gpui::ModelContext<T>) -> Option<gpui::Subscription> {
Some(cx.observe(&self.state, |_, _, cx| {
fn observable_entity(&self) -> Option<gpui::Model<Self::ObservableEntity>> { cx.notify();
Some(self.state.clone()) }))
} }
} }

View file

@ -54,7 +54,9 @@ fn register_language_model_providers(
registry.register_provider(CloudLanguageModelProvider::new(client.clone(), cx), cx); registry.register_provider(CloudLanguageModelProvider::new(client.clone(), cx), cx);
} else { } else {
registry.unregister_provider( registry.unregister_provider(
&LanguageModelProviderId::from(crate::provider::cloud::PROVIDER_ID.to_string()), &LanguageModelProviderId::from(
crate::provider::cloud::PROVIDER_NAME.to_string(),
),
cx, cx,
); );
} }
@ -78,12 +80,9 @@ pub struct ActiveModel {
model: Option<Arc<dyn LanguageModel>>, model: Option<Arc<dyn LanguageModel>>,
} }
pub enum Event { pub struct ActiveModelChanged;
ActiveModelChanged,
ProviderStateChanged,
}
impl EventEmitter<Event> for LanguageModelRegistry {} impl EventEmitter<ActiveModelChanged> for LanguageModelRegistry {}
impl LanguageModelRegistry { impl LanguageModelRegistry {
pub fn global(cx: &AppContext) -> Model<Self> { pub fn global(cx: &AppContext) -> Model<Self> {
@ -115,10 +114,7 @@ impl LanguageModelRegistry {
) { ) {
let name = provider.id(); let name = provider.id();
let subscription = provider.subscribe(cx, |_, cx| { if let Some(subscription) = provider.subscribe(cx) {
cx.emit(Event::ProviderStateChanged);
});
if let Some(subscription) = subscription {
subscription.detach(); subscription.detach();
} }
@ -191,7 +187,7 @@ impl LanguageModelRegistry {
provider, provider,
model: None, model: None,
}); });
cx.emit(Event::ActiveModelChanged); cx.emit(ActiveModelChanged);
} }
pub fn set_active_model( pub fn set_active_model(
@ -206,13 +202,13 @@ impl LanguageModelRegistry {
provider, provider,
model: Some(model), model: Some(model),
}); });
cx.emit(Event::ActiveModelChanged); cx.emit(ActiveModelChanged);
} else { } else {
log::warn!("Active model's provider not found in registry"); log::warn!("Active model's provider not found in registry");
} }
} else { } else {
self.active_model = None; self.active_model = None;
cx.emit(Event::ActiveModelChanged); cx.emit(ActiveModelChanged);
} }
} }