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: show local ai disable

Nathanb63c4dfe2124
diff --git a/frontend/appflowy_flutter/lib/ai/service/appflowy_ai_service.dart b/frontend/appflowy_flutter/lib/ai/service/appflowy_ai_service.dart
index d96a3f1eed..39487652f8 100644
--- a/frontend/appflowy_flutter/lib/ai/service/appflowy_ai_service.dart
+++ b/frontend/appflowy_flutter/lib/ai/service/appflowy_ai_service.dart
@@ -15,6 +15,11 @@ import 'package:fixnum/fixnum.dart' as fixnum;
import 'ai_entities.dart';
import 'error.dart';
+enum LocalAIStreamingState {
+ notReady,
+ disabled,
+}
+
abstract class AIRepository {
Future<void> streamCompletion({
String? objectId,
@@ -28,7 +33,8 @@ abstract class AIRepository {
required Future<void> Function(String text) processAssistMessage,
required Future<void> Function() onEnd,
required void Function(AIError error) onError,
- required void Function() onLocalAIInitializing,
+ required void Function(LocalAIStreamingState state)
+ onLocalAIStreamingStateChange,
});
}
@@ -46,14 +52,15 @@ class AppFlowyAIService implements AIRepository {
required Future<void> Function(String text) processAssistMessage,
required Future<void> Function() onEnd,
required void Function(AIError error) onError,
- required void Function() onLocalAIInitializing,
+ required void Function(LocalAIStreamingState state)
+ onLocalAIStreamingStateChange,
}) async {
final stream = AppFlowyCompletionStream(
onStart: onStart,
processMessage: processMessage,
processAssistMessage: processAssistMessage,
processError: onError,
- onLocalAIInitializing: onLocalAIInitializing,
+ onLocalAIStreamingStateChange: onLocalAIStreamingStateChange,
onEnd: onEnd,
);
@@ -88,7 +95,7 @@ abstract class CompletionStream {
required this.processMessage,
required this.processAssistMessage,
required this.processError,
- required this.onLocalAIInitializing,
+ required this.onLocalAIStreamingStateChange,
required this.onEnd,
});
@@ -96,7 +103,8 @@ abstract class CompletionStream {
final Future<void> Function(String text) processMessage;
final Future<void> Function(String text) processAssistMessage;
final void Function(AIError error) processError;
- final void Function() onLocalAIInitializing;
+ final void Function(LocalAIStreamingState state)
+ onLocalAIStreamingStateChange;
final Future<void> Function() onEnd;
}
@@ -107,7 +115,7 @@ class AppFlowyCompletionStream extends CompletionStream {
required super.processAssistMessage,
required super.processError,
required super.onEnd,
- required super.onLocalAIInitializing,
+ required super.onLocalAIStreamingStateChange,
}) {
_startListening();
}
@@ -155,43 +163,42 @@ class AppFlowyCompletionStream extends CompletionStream {
}
// Otherwise, parse out prefix:content
- final colonIndex = event.indexOf(':');
- final hasColon = colonIndex != -1;
- final prefix = hasColon ? event.substring(0, colonIndex) : event;
- final content = hasColon ? event.substring(colonIndex + 1) : '';
-
- switch (prefix) {
- case AIStreamEventPrefix.aiMaxRequired:
- processError(AIError(message: content, code: AIErrorCode.other));
- break;
-
- case AIStreamEventPrefix.start:
- await onStart();
- break;
-
- case AIStreamEventPrefix.data:
- await processMessage(content);
- break;
-
- case AIStreamEventPrefix.comment:
- await processAssistMessage(content);
- break;
-
- case AIStreamEventPrefix.finish:
- await onEnd();
- break;
-
- case AIStreamEventPrefix.localAINotReady:
- onLocalAIInitializing();
- break;
-
- case AIStreamEventPrefix.error:
- processError(AIError(message: content, code: AIErrorCode.other));
- break;
-
- default:
- Log.debug('Unknown AI event: $event');
- break;
+ if (event.startsWith(AIStreamEventPrefix.aiMaxRequired)) {
+ processError(
+ AIError(
+ message: event.substring(AIStreamEventPrefix.aiMaxRequired.length),
+ code: AIErrorCode.other,
+ ),
+ );
+ } else if (event.startsWith(AIStreamEventPrefix.start)) {
+ await onStart();
+ } else if (event.startsWith(AIStreamEventPrefix.data)) {
+ await processMessage(
+ event.substring(AIStreamEventPrefix.data.length),
+ );
+ } else if (event.startsWith(AIStreamEventPrefix.comment)) {
+ await processAssistMessage(
+ event.substring(AIStreamEventPrefix.comment.length),
+ );
+ } else if (event.startsWith(AIStreamEventPrefix.finish)) {
+ await onEnd();
+ } else if (event.startsWith(AIStreamEventPrefix.localAIDisabled)) {
+ onLocalAIStreamingStateChange(
+ LocalAIStreamingState.disabled,
+ );
+ } else if (event.startsWith(AIStreamEventPrefix.localAINotReady)) {
+ onLocalAIStreamingStateChange(
+ LocalAIStreamingState.notReady,
+ );
+ } else if (event.startsWith(AIStreamEventPrefix.error)) {
+ processError(
+ AIError(
+ message: event.substring(AIStreamEventPrefix.error.length),
+ code: AIErrorCode.other,
+ ),
+ );
+ } else {
+ Log.debug('Unknown AI event: $event');
}
}
}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart
index 56b4934e3d..bfd7c24fea 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart
@@ -568,7 +568,13 @@ class MainContentArea extends StatelessWidget {
),
);
}
- if (state is LocalAIRunningAiWriterState) {
+ if (state is LocalAIStreamingAiWriterState) {
+ final text = switch (state.state) {
+ LocalAIStreamingState.notReady =>
+ LocaleKeys.settings_aiPage_keys_localAINotReadyRetryLater.tr(),
+ LocalAIStreamingState.disabled =>
+ LocaleKeys.settings_aiPage_keys_localAIDisabled.tr(),
+ };
return Padding(
padding: EdgeInsets.all(8.0),
child: Row(
@@ -576,10 +582,7 @@ class MainContentArea extends StatelessWidget {
const HSpace(8.0),
Opacity(
opacity: 0.5,
- child: FlowyText(
- LocaleKeys.settings_aiPage_keys_localAINotReadyRetryLater
- .tr(),
- ),
+ child: FlowyText(text),
),
],
),
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_cubit.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_cubit.dart
index fae66d316c..076046624a 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_cubit.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_cubit.dart
@@ -390,8 +390,8 @@ class AiWriterCubit extends Cubit<AiWriterState> {
AiWriterRecord.ai(content: _textRobot.markdownText),
);
},
- onLocalAIInitializing: () {
- emit(LocalAIRunningAiWriterState(command));
+ onLocalAIStreamingStateChange: (state) {
+ emit(LocalAIStreamingAiWriterState(command, state: state));
},
);
@@ -484,8 +484,8 @@ class AiWriterCubit extends Cubit<AiWriterState> {
AiWriterRecord.ai(content: _textRobot.markdownText),
);
},
- onLocalAIInitializing: () {
- emit(LocalAIRunningAiWriterState(command));
+ onLocalAIStreamingStateChange: (state) {
+ emit(LocalAIStreamingAiWriterState(command, state: state));
},
);
if (stream != null) {
@@ -575,8 +575,8 @@ class AiWriterCubit extends Cubit<AiWriterState> {
AiWriterRecord.ai(content: _textRobot.markdownText),
);
},
- onLocalAIInitializing: () {
- emit(LocalAIRunningAiWriterState(command));
+ onLocalAIStreamingStateChange: (state) {
+ emit(LocalAIStreamingAiWriterState(command, state: state));
},
);
if (stream != null) {
@@ -648,8 +648,8 @@ class AiWriterCubit extends Cubit<AiWriterState> {
}
emit(ErrorAiWriterState(command, error: error));
},
- onLocalAIInitializing: () {
- emit(LocalAIRunningAiWriterState(command));
+ onLocalAIStreamingStateChange: (state) {
+ emit(LocalAIStreamingAiWriterState(command, state: state));
},
);
if (stream != null) {
@@ -727,10 +727,15 @@ class DocumentContentEmptyAiWriterState extends AiWriterState
final void Function() onConfirm;
}
-class LocalAIRunningAiWriterState extends AiWriterState
+class LocalAIStreamingAiWriterState extends AiWriterState
with RegisteredAiWriter {
- const LocalAIRunningAiWriterState(this.command);
+ const LocalAIStreamingAiWriterState(
+ this.command, {
+ required this.state,
+ });
@override
final AiWriterCommand command;
+
+ final LocalAIStreamingState state;
}
diff --git a/frontend/appflowy_flutter/test/bloc_test/ai_writer_test/ai_writer_bloc_test.dart b/frontend/appflowy_flutter/test/bloc_test/ai_writer_test/ai_writer_bloc_test.dart
index 0cd494dd4d..bcd8b13d39 100644
--- a/frontend/appflowy_flutter/test/bloc_test/ai_writer_test/ai_writer_bloc_test.dart
+++ b/frontend/appflowy_flutter/test/bloc_test/ai_writer_test/ai_writer_bloc_test.dart
@@ -30,7 +30,8 @@ class _MockAIRepository extends Mock implements AppFlowyAIService {
required Future<void> Function(String text) processAssistMessage,
required Future<void> Function() onEnd,
required void Function(AIError error) onError,
- required void Function() onLocalAIInitializing,
+ required void Function(LocalAIStreamingState state)
+ onLocalAIStreamingStateChange,
}) async {
final stream = _MockCompletionStream();
unawaited(
@@ -63,7 +64,8 @@ class _MockAIRepositoryLess extends Mock implements AppFlowyAIService {
required Future<void> Function(String text) processAssistMessage,
required Future<void> Function() onEnd,
required void Function(AIError error) onError,
- required void Function() onLocalAIInitializing,
+ required void Function(LocalAIStreamingState state)
+ onLocalAIStreamingStateChange,
}) async {
final stream = _MockCompletionStream();
unawaited(
@@ -92,7 +94,8 @@ class _MockAIRepositoryMore extends Mock implements AppFlowyAIService {
required Future<void> Function(String text) processAssistMessage,
required Future<void> Function() onEnd,
required void Function(AIError error) onError,
- required void Function() onLocalAIInitializing,
+ required void Function(LocalAIStreamingState state)
+ onLocalAIStreamingStateChange,
}) async {
final stream = _MockCompletionStream();
unawaited(
@@ -123,7 +126,8 @@ class _MockErrorRepository extends Mock implements AppFlowyAIService {
required Future<void> Function(String text) processAssistMessage,
required Future<void> Function() onEnd,
required void Function(AIError error) onError,
- required void Function() onLocalAIInitializing,
+ required void Function(LocalAIStreamingState state)
+ onLocalAIStreamingStateChange,
}) async {
final stream = _MockCompletionStream();
unawaited(
diff --git a/frontend/resources/translations/en.json b/frontend/resources/translations/en.json
index 72b46000a2..e93f5273e5 100644
--- a/frontend/resources/translations/en.json
+++ b/frontend/resources/translations/en.json
@@ -860,6 +860,7 @@
"localAIRunning": "Local AI is running",
"localAIInitializing": "Local AI is loading and may take a few seconds, depending on your device",
"localAINotReadyRetryLater": "Local AI is initializing, please retry later",
+ "localAIDisabled": "You are using local AI, but it is disabled. Please go to settings to enable it or try different model",
"localAINotReadyTextFieldPrompt": "You can not edit while Local AI is loading",
"failToLoadLocalAI": "Failed to start local AI",
"restartLocalAI": "Restart Local AI",
diff --git a/frontend/rust-lib/flowy-ai/src/ai_manager.rs b/frontend/rust-lib/flowy-ai/src/ai_manager.rs
index eabf41a1e3..d8ef5977d8 100644
--- a/frontend/rust-lib/flowy-ai/src/ai_manager.rs
+++ b/frontend/rust-lib/flowy-ai/src/ai_manager.rs
@@ -246,7 +246,8 @@ impl AIManager {
params: StreamMessageParams,
) -> Result<ChatMessagePB, FlowyError> {
let chat = self.get_or_create_chat_instance(&params.chat_id).await?;
- let question = chat.stream_chat_message(&params).await?;
+ let ai_model = self.get_active_model(&params.chat_id).await;
+ let question = chat.stream_chat_message(&params, ai_model).await?;
let _ = self
.external_service
.notify_did_send_message(&params.chat_id, &params.message)
@@ -394,6 +395,20 @@ impl AIManager {
Ok(())
}
+ pub async fn get_active_model(&self, source: &str) -> Option<AIModel> {
+ let mut model = self
+ .store_preferences
+ .get_object::<AIModel>(&ai_available_models_key(source));
+
+ if model.is_none() {
+ if let Some(local_model) = self.local_ai.get_plugin_chat_model() {
+ model = Some(AIModel::local(local_model, "".to_string()));
+ }
+ }
+
+ model
+ }
+
pub async fn get_available_models(&self, source: String) -> FlowyResult<AvailableModelsPB> {
// Build the models list from server models and mark them as non-local.
let mut models: Vec<AIModel> = self
diff --git a/frontend/rust-lib/flowy-ai/src/chat.rs b/frontend/rust-lib/flowy-ai/src/chat.rs
index e656aefa1e..50cec2c625 100644
--- a/frontend/rust-lib/flowy-ai/src/chat.rs
+++ b/frontend/rust-lib/flowy-ai/src/chat.rs
@@ -10,7 +10,6 @@ use crate::persistence::{
ChatMessageTable,
};
use crate::stream_message::StreamMessage;
-use crate::util::ai_available_models_key;
use allo_isolate::Isolate;
use flowy_ai_pub::cloud::{
AIModel, ChatCloudService, ChatMessage, MessageCursor, QuestionStreamValue, ResponseFormat,
@@ -89,6 +88,7 @@ impl Chat {
pub async fn stream_chat_message(
&self,
params: &StreamMessageParams,
+ preferred_ai_model: Option<AIModel>,
) -> Result<ChatMessagePB, FlowyError> {
trace!(
"[Chat] stream chat message: chat_id={}, message={}, message_type={:?}, metadata={:?}, format={:?}",
@@ -143,9 +143,6 @@ impl Chat {
// Save message to disk
save_and_notify_message(uid, &self.chat_id, &self.user_service, question.clone())?;
let format = params.format.clone().map(Into::into).unwrap_or_default();
- let preferred_ai_model = self
- .store_preferences
- .get_object::<AIModel>(&ai_available_models_key(&self.chat_id));
self.stream_response(
params.answer_stream_port,
answer_stream_buffer,
diff --git a/frontend/rust-lib/flowy-ai/src/completion.rs b/frontend/rust-lib/flowy-ai/src/completion.rs
index 8e505a49b9..393685d642 100644
--- a/frontend/rust-lib/flowy-ai/src/completion.rs
+++ b/frontend/rust-lib/flowy-ai/src/completion.rs
@@ -13,7 +13,6 @@ use futures::{SinkExt, StreamExt};
use lib_infra::isolate_stream::IsolateSink;
use crate::stream_message::StreamMessage;
-use crate::util::ai_available_models_key;
use flowy_sqlite::kv::KVStorePreferences;
use std::sync::{Arc, Weak};
use tokio::select;
@@ -23,26 +22,24 @@ pub struct AICompletion {
tasks: Arc<DashMap<String, tokio::sync::mpsc::Sender<()>>>,
cloud_service: Weak<dyn ChatCloudService>,
user_service: Weak<dyn AIUserService>,
- store_preferences: Arc<KVStorePreferences>,
}
impl AICompletion {
pub fn new(
cloud_service: Weak<dyn ChatCloudService>,
user_service: Weak<dyn AIUserService>,
- store_preferences: Arc<KVStorePreferences>,
) -> Self {
Self {
tasks: Arc::new(DashMap::new()),
cloud_service,
user_service,
- store_preferences,
}
}
pub async fn create_complete_task(
&self,
complete: CompleteTextPB,
+ preferred_model: Option<AIModel>,
) -> FlowyResult<CompleteTextTaskPB> {
if matches!(complete.completion_type, CompletionTypePB::CustomPrompt)
&& complete.custom_prompt.is_none()
@@ -59,10 +56,6 @@ impl AICompletion {
.ok_or_else(FlowyError::internal)?
.workspace_id()?;
let (tx, rx) = tokio::sync::mpsc::channel(1);
- let preferred_model = self
- .store_preferences
- .get_object::<AIModel>(&ai_available_models_key(&complete.object_id));
-
let task = CompletionTask::new(
workspace_id,
complete,
diff --git a/frontend/rust-lib/flowy-ai/src/event_handler.rs b/frontend/rust-lib/flowy-ai/src/event_handler.rs
index 3150d3e378..4a36a5d37a 100644
--- a/frontend/rust-lib/flowy-ai/src/event_handler.rs
+++ b/frontend/rust-lib/flowy-ai/src/event_handler.rs
@@ -209,9 +209,13 @@ pub(crate) async fn stop_stream_handler(
pub(crate) async fn start_complete_text_handler(
data: AFPluginData<CompleteTextPB>,
+ ai_manager: AFPluginState<Weak<AIManager>>,
tools: AFPluginState<Arc<AICompletion>>,
) -> DataResult<CompleteTextTaskPB, FlowyError> {
- let task = tools.create_complete_task(data.into_inner()).await?;
+ let data = data.into_inner();
+ let ai_manager = upgrade_ai_manager(ai_manager)?;
+ let ai_model = ai_manager.get_active_model(&data.object_id).await;
+ let task = tools.create_complete_task(data, ai_model).await?;
data_result_ok(task)
}
diff --git a/frontend/rust-lib/flowy-ai/src/event_map.rs b/frontend/rust-lib/flowy-ai/src/event_map.rs
index 51c49eaabb..5020836a30 100644
--- a/frontend/rust-lib/flowy-ai/src/event_map.rs
+++ b/frontend/rust-lib/flowy-ai/src/event_map.rs
@@ -13,11 +13,7 @@ pub fn init(ai_manager: Weak<AIManager>) -> AFPlugin {
let strong_ai_manager = ai_manager.upgrade().unwrap();
let user_service = Arc::downgrade(&strong_ai_manager.user_service);
let cloud_service = Arc::downgrade(&strong_ai_manager.cloud_service_wm);
- let ai_tools = Arc::new(AICompletion::new(
- cloud_service,
- user_service,
- strong_ai_manager.store_preferences.clone(),
- ));
+ let ai_tools = Arc::new(AICompletion::new(cloud_service, user_service));
AFPlugin::new()
.name("flowy-ai")
.state(ai_manager)