Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

chore: ai setting ui

Nathan8b2e769fca84
diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_bloc.dart
index 3c3d20039d..66ba7fe572 100644
--- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_bloc.dart
+++ b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_bloc.dart
@@ -52,7 +52,8 @@ class LocalAIToggleBloc extends Bloc<LocalAIToggleEvent, LocalAIToggleState> {
(localAI) {
emit(
state.copyWith(
- pageIndicator: LocalAIToggleStateIndicator.ready(localAI.enabled),
+ pageIndicator:
+ LocalAIToggleStateIndicator.isEnabled(localAI.enabled),
),
);
},
@@ -88,6 +89,6 @@ class LocalAIToggleState with _$LocalAIToggleState {
class LocalAIToggleStateIndicator with _$LocalAIToggleStateIndicator {
// when start downloading the model
const factory LocalAIToggleStateIndicator.error(FlowyError error) = _OnError;
- const factory LocalAIToggleStateIndicator.ready(bool isEnabled) = _Ready;
+ const factory LocalAIToggleStateIndicator.isEnabled(bool isEnabled) = _Ready;
const factory LocalAIToggleStateIndicator.loading() = _Loading;
}
diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting.dart
index 706ecdd1a1..2a88008565 100644
--- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting.dart
+++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting.dart
@@ -38,10 +38,11 @@ class _LocalAISettingState extends State<LocalAISetting> {
listener: (context, state) {
final controller =
ExpandableController.of(context, required: true)!;
+
state.pageIndicator.when(
- error: (_) => controller.expanded = false,
- ready: (enabled) => controller.expanded = enabled,
- loading: () => controller.expanded = false,
+ error: (_) => controller.expanded = true,
+ isEnabled: (enabled) => controller.expanded = enabled,
+ loading: () => controller.expanded = true,
);
},
child: ExpandablePanel(
@@ -95,9 +96,9 @@ class LocalAISettingHeader extends StatelessWidget {
return const SizedBox.shrink();
},
loading: () {
- return const CircularProgressIndicator.adaptive();
+ return const SizedBox.shrink();
},
- ready: (isEnabled) {
+ isEnabled: (isEnabled) {
return Row(
children: [
FlowyText(
diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting_panel.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting_panel.dart
index 66379d347a..cdbba0f28a 100644
--- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting_panel.dart
+++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting_panel.dart
@@ -21,70 +21,41 @@ class LocalAISettingPanel extends StatelessWidget {
),
],
child: ExpandableNotifier(
- child: BlocListener<LocalAIChatToggleBloc, LocalAIChatToggleState>(
- listener: (context, state) {
- // Listen to the toggle state and expand the panel if the state is ready.
- final controller = ExpandableController.of(
- context,
- required: true,
- )!;
-
- // Neet to wrap with WidgetsBinding.instance.addPostFrameCallback otherwise the
- // ExpandablePanel not expanded sometimes. Maybe because the ExpandablePanel is not
- // built yet when the listener is called.
- WidgetsBinding.instance.addPostFrameCallback(
- (_) {
- state.pageIndicator.when(
- error: (_) => controller.expanded = false,
- ready: (enabled) {
- controller.expanded = enabled;
- context.read<LocalAISettingPanelBloc>().add(
- const LocalAISettingPanelEvent.started(),
- );
+ initialExpanded: true,
+ child: ExpandablePanel(
+ theme: const ExpandableThemeData(
+ headerAlignment: ExpandablePanelHeaderAlignment.center,
+ tapBodyToCollapse: false,
+ hasIcon: false,
+ tapBodyToExpand: false,
+ tapHeaderToExpand: false,
+ ),
+ header: const SizedBox.shrink(),
+ collapsed: const SizedBox.shrink(),
+ expanded: Padding(
+ padding: const EdgeInsets.symmetric(vertical: 6),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ BlocBuilder<LocalAISettingPanelBloc, LocalAISettingPanelState>(
+ builder: (context, state) {
+ // If the progress indicator is startLocalAIApp, then don't show the LLM model.
+ if (state.progressIndicator ==
+ const LocalAIProgress.downloadLocalAIApp()) {
+ return const SizedBox.shrink();
+ } else {
+ return Column(
+ mainAxisAlignment: MainAxisAlignment.spaceBetween,
+ children: const [
+ OllamaSettingPage(),
+ VSpace(6),
+ _LocalAIStateWidget(),
+ ],
+ );
+ }
},
- loading: () => controller.expanded = false,
- );
- },
- debugLabel: 'LocalAI.showLocalAIChatSetting',
- );
- },
- child: ExpandablePanel(
- theme: const ExpandableThemeData(
- headerAlignment: ExpandablePanelHeaderAlignment.center,
- tapBodyToCollapse: false,
- hasIcon: false,
- tapBodyToExpand: false,
- tapHeaderToExpand: false,
- ),
- header: const SizedBox.shrink(),
- collapsed: const SizedBox.shrink(),
- expanded: Padding(
- padding: const EdgeInsets.symmetric(vertical: 6),
- // child: _LocalLLMInfoWidget(),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- BlocBuilder<LocalAISettingPanelBloc,
- LocalAISettingPanelState>(
- builder: (context, state) {
- // If the progress indicator is startLocalAIApp, then don't show the LLM model.
- if (state.progressIndicator ==
- const LocalAIProgress.downloadLocalAIApp()) {
- return const SizedBox.shrink();
- } else {
- return Column(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: const [
- OllamaSettingPage(),
- VSpace(6),
- _LocalAIStateWidget(),
- ],
- );
- }
- },
- ),
- ],
- ),
+ ),
+ ],
),
),
),
@@ -117,7 +88,7 @@ class _LocalAIStateWidget extends StatelessWidget {
child: child,
);
} else {
- return const SizedBox.shrink();
+ return const PluginStateIndicator();
}
},
);
diff --git a/frontend/resources/translations/en.json b/frontend/resources/translations/en.json
index e6a5658865..66625d124d 100644
--- a/frontend/resources/translations/en.json
+++ b/frontend/resources/translations/en.json
@@ -868,7 +868,8 @@
"openModelDirectory": "Open folder",
"localAISetupInstruction1": "Follow the",
"localAISetupInstruction2": "instruction",
- "localAISetupInstruction3": "to setup your Ollama"
+ "localAISetupInstruction3": "to setup your Ollama",
+ "startLocalAI": "It may take a few seconds to start the local AI"
}
},
"planPage": {
diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs b/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs
index 493b744152..7fff47c89d 100644
--- a/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs
+++ b/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs
@@ -44,7 +44,6 @@ impl Default for LocalAISetting {
}
}
-const APPFLOWY_LOCAL_AI_ENABLED: &str = "appflowy_local_ai_enabled";
const LOCAL_AI_SETTING_KEY: &str = "appflowy_local_ai_setting:v1";
pub struct LocalAIController {
@@ -87,27 +86,29 @@ impl LocalAIController {
let mut running_state_rx = local_ai.subscribe_running_state();
let cloned_llm_res = local_ai_resource.clone();
let cloned_store_preferences = store_preferences.clone();
+ let cloned_user_service = user_service.clone();
tokio::spawn(async move {
while let Some(state) = running_state_rx.next().await {
- info!("[AI Plugin] state: {:?}", state);
- let ready = cloned_llm_res.is_app_downloaded();
- let lack_of_resource = cloned_llm_res.get_lack_of_resource().await;
-
- let new_state = RunningStatePB::from(state);
- let enabled = cloned_store_preferences
- .get_bool(APPFLOWY_LOCAL_AI_ENABLED)
- .unwrap_or(true);
- chat_notification_builder(
- APPFLOWY_AI_NOTIFICATION_KEY,
- ChatNotification::UpdateLocalAIState,
- )
- .payload(LocalAIPB {
- enabled,
- is_app_downloaded: ready,
- lack_of_resource,
- state: new_state,
- })
- .send();
+ if let Ok(workspace_id) = cloned_user_service.workspace_id() {
+ let key = local_ai_enabled_key(&workspace_id);
+ info!("[AI Plugin] state: {:?}", state);
+ let ready = cloned_llm_res.is_app_downloaded();
+ let lack_of_resource = cloned_llm_res.get_lack_of_resource().await;
+
+ let new_state = RunningStatePB::from(state);
+ let enabled = cloned_store_preferences.get_bool(&key).unwrap_or(true);
+ chat_notification_builder(
+ APPFLOWY_AI_NOTIFICATION_KEY,
+ ChatNotification::UpdateLocalAIState,
+ )
+ .payload(LocalAIPB {
+ enabled,
+ is_app_downloaded: ready,
+ lack_of_resource,
+ state: new_state,
+ })
+ .send();
+ }
}
});
@@ -428,8 +429,21 @@ impl LocalAIController {
error!("[AI Plugin] failed to initialize local ai: {:?}", err);
}
let _ = rx.await;
- } else if let Err(err) = self.ai_plugin.destroy_chat_plugin().await {
- error!("[AI Plugin] failed to destroy plugin: {:?}", err);
+ } else {
+ if let Err(err) = self.ai_plugin.destroy_chat_plugin().await {
+ error!("[AI Plugin] failed to destroy plugin: {:?}", err);
+ }
+ chat_notification_builder(
+ APPFLOWY_AI_NOTIFICATION_KEY,
+ ChatNotification::UpdateLocalAIState,
+ )
+ .payload(LocalAIPB {
+ enabled,
+ is_app_downloaded: true,
+ state: RunningStatePB::Stopped,
+ lack_of_resource: None,
+ })
+ .send();
}
Ok(())
}
@@ -537,6 +551,7 @@ impl LLMResourceService for LLMResourceServiceImpl {
}
}
+const APPFLOWY_LOCAL_AI_ENABLED: &str = "appflowy_local_ai_enabled";
fn local_ai_enabled_key(workspace_id: &str) -> String {
format!("{}:{}", APPFLOWY_LOCAL_AI_ENABLED, workspace_id)
}