From 2bd15a7d832bb49c9a5a43eb9475dde1cb4696b3 Mon Sep 17 00:00:00 2001 From: Vika Date: Thu, 22 Aug 2024 18:30:14 +0300 Subject: SmartSummaryButton: un-asyncify and move summarization to a command What this command should do is construct a summarization request and return a future which would return chunks from the LLM. Perhaps this component will be asyncified in the future. --- src/components/smart_summary.rs | 65 ++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 21 deletions(-) (limited to 'src/components') diff --git a/src/components/smart_summary.rs b/src/components/smart_summary.rs index b360d77..fbfc80b 100644 --- a/src/components/smart_summary.rs +++ b/src/components/smart_summary.rs @@ -1,5 +1,5 @@ use adw::prelude::*; -use relm4::{gtk, prelude::{AsyncComponent, AsyncComponentParts}, AsyncComponentSender}; +use relm4::{gtk, prelude::{Component, ComponentParts}, ComponentSender}; #[derive(Debug, Default)] pub(crate) struct SmartSummaryButton { @@ -13,8 +13,9 @@ pub(crate) enum Error { #[derive(Debug)] pub(crate) enum Input { - ButtonPressed, - Text(String) + #[doc(hidden)] ButtonPressed, + Text(String), + Cancel, } #[derive(Debug)] @@ -26,13 +27,13 @@ pub(crate) enum Output { Error(Error) } -#[relm4::component(pub(crate) async)] -impl AsyncComponent for SmartSummaryButton { +#[relm4::component(pub(crate))] +impl Component for SmartSummaryButton { type Input = Input; type Output = Output; type Init = (); - type CommandOutput = (); + type CommandOutput = Result; view! { #[root] @@ -52,44 +53,66 @@ impl AsyncComponent for SmartSummaryButton { } } - async fn init( + fn init( _init: Self::Init, root: Self::Root, - sender: AsyncComponentSender - ) -> AsyncComponentParts { + sender: ComponentSender + ) -> ComponentParts { let model = Self::default(); let widgets = view_output!(); - AsyncComponentParts { model, widgets } + ComponentParts { model, widgets } } - async fn update_with_view( + fn update( &mut self, - widgets: &mut Self::Widgets, msg: Self::Input, - sender: AsyncComponentSender, + sender: ComponentSender, _root: &Self::Root ) { match msg { + Input::Cancel => { + self.busy = false; + log::debug!("Parent component asked us to cancel."); + }, Input::ButtonPressed => if let Ok(()) = sender.output(Output::Start) { self.busy = true; log::debug!("Requesting text to summarize from parent component..."); - self.update_view(widgets, sender.clone()); }, Input::Text(text) => { log::debug!("Would generate summary for the following text:\n{}", text); - tokio::time::sleep(std::time::Duration::from_millis(450)).await; - for i in ["I'", "m ", "sorry,", " I", " am ", "unable", " to", " ", "write", " you ", "a summary.", " I", " am", " not ", "really ", "an ", "LLM."] { - tokio::time::sleep(std::time::Duration::from_millis(100)).await; - let _ = sender.output(Output::Chunk(i.to_string())); - } + sender.command(|sender, shutdown| shutdown.register(async move { + tokio::time::sleep(std::time::Duration::from_millis(450)).await; + + for i in ["I'", "m ", "sorry,", " I", " am ", "unable", " to", " ", "write", " you ", "a summary.", " I", " am", " not ", "really ", "an ", "LLM."] { + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + + if sender.send(Ok(i.to_string())).is_err() { + return + }; + } + + log::debug!("Done with the summary."); + let _ = sender.send(Ok(String::new())); + }).drop_on_shutdown()); + } + } + } - log::debug!("Done with the summary."); + fn update_cmd(&mut self, msg: Self::CommandOutput, sender: ComponentSender, _root: &Self::Root) { + match msg { + Ok(chunk) if chunk.is_empty() => { self.busy = false; let _ = sender.output(Output::Done); - self.update_view(widgets, sender.clone()); + }, + Err(err) => { + self.busy = false; + let _ = sender.output(Output::Error(err)); } + Ok(chunk) => { + let _ = sender.output(Output::Chunk(chunk)); + }, } } } -- cgit 1.4.1