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.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
chore: implement ollama
Nathan4ff71b5dcef2
diff --git a/frontend/appflowy_flutter/lib/ai/service/ai_prompt_input_bloc.dart b/frontend/appflowy_flutter/lib/ai/service/ai_prompt_input_bloc.dartindex 23d8879275..2dff6ccbd2 100644--- a/frontend/appflowy_flutter/lib/ai/service/ai_prompt_input_bloc.dart+++ b/frontend/appflowy_flutter/lib/ai/service/ai_prompt_input_bloc.dart@@ -36,36 +36,19 @@ class AIPromptInputBloc extends Bloc<AIPromptInputEvent, AIPromptInputState> {on<AIPromptInputEvent>((event, emit) {event.when(- updateChatState: (LocalAIChatPB chatState) {+ updateAIState: (LocalAIPB localAIState) {// Only user enable chat with file and the plugin is already running- final supportChatWithFile = chatState.fileEnabled &&- chatState.pluginState.state == RunningStatePB.Running;+ final supportChatWithFile = localAIState.enabled &&+ localAIState.state == RunningStatePB.Running;- final aiType = chatState.pluginState.state == RunningStatePB.Running- ? AIType.localAI- : AIType.appflowyAI;+ final aiType =+ localAIState.enabled ? AIType.localAI : AIType.appflowyAI;emit(state.copyWith(aiType: aiType,supportChatWithFile: supportChatWithFile,- chatState: chatState,- ),- );- },- updatePluginState: (LocalAIPluginStatePB chatState) {- final fileEnabled = state.chatState?.fileEnabled ?? false;- final supportChatWithFile =- fileEnabled && chatState.state == RunningStatePB.Running;-- final aiType = chatState.state == RunningStatePB.Running- ? AIType.localAI- : AIType.appflowyAI;-- emit(- state.copyWith(- supportChatWithFile: supportChatWithFile,- aiType: aiType,+ localAIState: localAIState,),);},@@ -130,22 +113,17 @@ class AIPromptInputBloc extends Bloc<AIPromptInputEvent, AIPromptInputState> {_listener.start(stateCallback: (pluginState) {if (!isClosed) {- add(AIPromptInputEvent.updatePluginState(pluginState));- }- },- chatStateCallback: (chatState) {- if (!isClosed) {- add(AIPromptInputEvent.updateChatState(chatState));+ add(AIPromptInputEvent.updateAIState(pluginState));}},);}void _init() {- AIEventGetLocalAIChatState().send().fold(- (chatState) {+ AIEventGetLocalAIState().send().fold(+ (localAIState) {if (!isClosed) {- add(AIPromptInputEvent.updateChatState(chatState));+ add(AIPromptInputEvent.updateAIState(localAIState));}},Log.error,@@ -168,12 +146,8 @@ class AIPromptInputBloc extends Bloc<AIPromptInputEvent, AIPromptInputState> {@freezedclass AIPromptInputEvent with _$AIPromptInputEvent {- const factory AIPromptInputEvent.updateChatState(- LocalAIChatPB chatState,- ) = _UpdateChatState;- const factory AIPromptInputEvent.updatePluginState(- LocalAIPluginStatePB chatState,- ) = _UpdatePluginState;+ const factory AIPromptInputEvent.updateAIState(LocalAIPB localAIState) =+ _UpdateAIState;const factory AIPromptInputEvent.toggleShowPredefinedFormat() =_ToggleShowPredefinedFormat;const factory AIPromptInputEvent.updatePredefinedFormat(@@ -196,7 +170,7 @@ class AIPromptInputState with _$AIPromptInputState {required bool supportChatWithFile,required bool showPredefinedFormats,required PredefinedFormat? predefinedFormat,- required LocalAIChatPB? chatState,+ required LocalAIPB? localAIState,required List<ChatFile> attachedFiles,required List<ViewPB> mentionedPages,}) = _AIPromptInputState;@@ -207,7 +181,7 @@ class AIPromptInputState with _$AIPromptInputState {supportChatWithFile: false,showPredefinedFormats: format != null,predefinedFormat: format,- chatState: null,+ localAIState: null,attachedFiles: [],mentionedPages: [],);diff --git a/frontend/appflowy_flutter/lib/ai/widgets/prompt_input/desktop_prompt_text_field.dart b/frontend/appflowy_flutter/lib/ai/widgets/prompt_input/desktop_prompt_text_field.dartindex e2eeb80434..0905e9b6bc 100644--- a/frontend/appflowy_flutter/lib/ai/widgets/prompt_input/desktop_prompt_text_field.dart+++ b/frontend/appflowy_flutter/lib/ai/widgets/prompt_input/desktop_prompt_text_field.dart@@ -572,7 +572,7 @@ class _PromptBottomActions extends StatelessWidget {margin: DesktopAIChatSizes.inputActionBarMargin,child: BlocBuilder<AIPromptInputBloc, AIPromptInputState>(builder: (context, state) {- if (state.chatState == null) {+ if (state.localAIState == null) {return Align(alignment: AlignmentDirectional.centerEnd,child: _sendButton(),diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/download_model_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/download_model_bloc.dartdeleted file mode 100644index 8be68e813e..0000000000--- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/download_model_bloc.dart+++ /dev/null@@ -1,167 +0,0 @@-import 'dart:async';-import 'dart:ffi';-import 'dart:isolate';--import 'package:appflowy/generated/locale_keys.g.dart';-import 'package:appflowy/plugins/ai_chat/application/chat_entity.dart';-import 'package:appflowy_backend/dispatch/dispatch.dart';-import 'package:appflowy_backend/log.dart';-import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart';-import 'package:bloc/bloc.dart';-import 'package:easy_localization/easy_localization.dart';-import 'package:freezed_annotation/freezed_annotation.dart';-import 'package:fixnum/fixnum.dart';-part 'download_model_bloc.freezed.dart';--class DownloadModelBloc extends Bloc<DownloadModelEvent, DownloadModelState> {- DownloadModelBloc(LLMModelPB model)- : super(DownloadModelState.initial(model)) {- on<DownloadModelEvent>(_handleEvent);- }-- Future<void> _handleEvent(- DownloadModelEvent event,- Emitter<DownloadModelState> emit,- ) async {- await event.when(- started: () async {- final downloadStream = DownloadingStream();- downloadStream.listen(- onModelPercentage: (name, percent) {- if (!isClosed) {- add(- DownloadModelEvent.updatePercent(name, percent),- );- }- },- onPluginPercentage: (percent) {- if (!isClosed) {- add(DownloadModelEvent.updatePercent("AppFlowy Plugin", percent));- }- },- onFinish: () {- add(const DownloadModelEvent.downloadFinish());- },- onError: (err) {- Log.error(err);- },- );-- final payload =- DownloadLLMPB(progressStream: Int64(downloadStream.nativePort));- final result = await AIEventDownloadLLMResource(payload).send();- result.fold((_) {- emit(- state.copyWith(- downloadStream: downloadStream,- loadingState: const ChatLoadingState.finish(),- downloadError: null,- ),- );- }, (err) {- emit(- state.copyWith(- loadingState: ChatLoadingState.finish(error: err),- ),- );- });- },- updatePercent: (String object, double percent) {- emit(state.copyWith(object: object, percent: percent));- },- downloadFinish: () {- emit(state.copyWith(isFinish: true));- },- );- }-- @override- Future<void> close() async {- await state.downloadStream?.dispose();- return super.close();- }-}--@freezed-class DownloadModelEvent with _$DownloadModelEvent {- const factory DownloadModelEvent.started() = _Started;- const factory DownloadModelEvent.updatePercent(- String object,- double percent,- ) = _UpdatePercent;- const factory DownloadModelEvent.downloadFinish() = _DownloadFinish;-}--@freezed-class DownloadModelState with _$DownloadModelState {- const factory DownloadModelState({- required LLMModelPB model,- DownloadingStream? downloadStream,- String? downloadError,- @Default("") String object,- @Default(0) double percent,- @Default(false) bool isFinish,- String? bigFileDownloadPrompt,- @Default(ChatLoadingState.loading()) ChatLoadingState loadingState,- }) = _DownloadModelState;-- factory DownloadModelState.initial(LLMModelPB model) {- // bigger than 1 GB then show download big file prompt- String? bigFileDownloadPrompt;- if (model.fileSize > 1 * 1024 * 1024 * 1024) {- bigFileDownloadPrompt =- LocaleKeys.settings_aiPage_keys_downloadBigFilePrompt.tr();- }- return DownloadModelState(- model: model,- bigFileDownloadPrompt: bigFileDownloadPrompt,- );- }-}--class DownloadingStream {- DownloadingStream() {- _port.handler = _controller.add;- }-- final RawReceivePort _port = RawReceivePort();- StreamSubscription<String>? _sub;- final StreamController<String> _controller = StreamController.broadcast();- int get nativePort => _port.sendPort.nativePort;-- Future<void> dispose() async {- await _sub?.cancel();- await _controller.close();- _port.close();- }-- void listen({- void Function(String modelName, double percent)? onModelPercentage,- void Function(double percent)? onPluginPercentage,- void Function(String data)? onError,- void Function()? onFinish,- }) {- _sub = _controller.stream.listen((text) {- if (text.contains(':progress:')) {- final progressIndex = text.indexOf(':progress:');- final modelName = text.substring(0, progressIndex);- final progressValue = text- .substring(progressIndex + 10); // 10 is the length of ":progress:"- final percent = double.tryParse(progressValue);- if (percent != null) {- onModelPercentage?.call(modelName, percent);- }- } else if (text.startsWith('plugin:progress:')) {- final percent = double.tryParse(text.substring(16));- if (percent != null) {- onPluginPercentage?.call(percent);- }- } else if (text.startsWith('finish')) {- onFinish?.call();- } else if (text.startsWith('error:')) {- // substring 6 to remove "error:"- onError?.call(text.substring(6));- }- });- }-}diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/download_offline_ai_app_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/download_offline_ai_app_bloc.dartindex 829bd2f62a..185a8c049f 100644--- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/download_offline_ai_app_bloc.dart+++ b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/download_offline_ai_app_bloc.dart@@ -18,7 +18,7 @@ class DownloadOfflineAIBloc) async {await event.when(started: () async {- final result = await AIEventGetOfflineAIAppLink().send();+ final result = await AIEventGetLocalAIDownloadLink().send();await result.fold((app) async {await launchUrl(Uri.parse(app.link));diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_chat_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_chat_bloc.dartindex 7f1df258ea..c254246213 100644--- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_chat_bloc.dart+++ b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_chat_bloc.dart@@ -1,185 +1,61 @@import 'dart:async';-import 'package:appflowy/plugins/ai_chat/application/chat_entity.dart';import 'package:appflowy/workspace/application/settings/ai/local_llm_listener.dart';import 'package:appflowy_backend/dispatch/dispatch.dart';import 'package:appflowy_backend/log.dart';import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart';-import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';import 'package:appflowy_result/appflowy_result.dart';import 'package:bloc/bloc.dart';import 'package:freezed_annotation/freezed_annotation.dart';part 'local_ai_chat_bloc.freezed.dart';-class LocalAIChatSettingBloc- extends Bloc<LocalAIChatSettingEvent, LocalAIChatSettingState> {- LocalAIChatSettingBloc()+class LocalAISettingPanelBloc+ extends Bloc<LocalAISettingPanelEvent, LocalAISettingPanelState> {+ LocalAISettingPanelBloc(): listener = LocalLLMListener(),- super(const LocalAIChatSettingState()) {+ super(const LocalAISettingPanelState()) {listener.start(stateCallback: (newState) {if (!isClosed) {- add(LocalAIChatSettingEvent.updatePluginState(newState));+ add(LocalAISettingPanelEvent.updateAIState(newState));}},);- on<LocalAIChatSettingEvent>(_handleEvent);+ on<LocalAISettingPanelEvent>(_handleEvent);}final LocalLLMListener listener;/// Handles incoming events and dispatches them to the appropriate handler.Future<void> _handleEvent(- LocalAIChatSettingEvent event,- Emitter<LocalAIChatSettingState> emit,+ LocalAISettingPanelEvent event,+ Emitter<LocalAISettingPanelState> emit,) async {- await event.when(- refreshAISetting: _handleStarted,- didLoadModelInfo: (FlowyResult<LLMModelInfoPB, FlowyError> result) {- result.fold(- (modelInfo) {- _fetchCurremtLLMState();- emit(- state.copyWith(- modelInfo: modelInfo,- models: modelInfo.models,- selectedLLMModel: modelInfo.selectedModel,- aiModelProgress: const AIModelProgress.finish(),- ),- );- },- (err) {- emit(- state.copyWith(- aiModelProgress: AIModelProgress.finish(error: err),- ),- );- },- );- },- selectLLMConfig: (LLMModelPB llmModel) async {- final result = await AIEventUpdateLocalLLM(llmModel).send();- result.fold(- (llmResource) {- // If all resources are downloaded, show reload plugin- if (llmResource.pendingResources.isNotEmpty) {- emit(- state.copyWith(- selectedLLMModel: llmModel,- progressIndicator: LocalAIProgress.showDownload(- llmResource,- llmModel,- ),- selectLLMState: const ChatLoadingState.finish(),- ),- );- } else {- emit(- state.copyWith(- selectedLLMModel: llmModel,- selectLLMState: const ChatLoadingState.finish(),- progressIndicator: const LocalAIProgress.checkPluginState(),- ),- );+ event.when(+ started: () {+ AIEventGetLocalAIState().send().fold(+ (localAIState) {+ if (!isClosed) {+ add(LocalAISettingPanelEvent.updateAIState(localAIState));}},- (err) {- emit(- state.copyWith(- selectLLMState: ChatLoadingState.finish(error: err),- ),- );- },+ Log.error,);},- refreshLLMState: (LocalModelResourcePB llmResource) {- if (state.selectedLLMModel == null) {- Log.error(- 'Unexpected null selected config. It should be set already',- );- return;- }-- // reload plugin if all resources are downloaded- if (llmResource.pendingResources.isEmpty) {+ updateAIState: (LocalAIPB pluginState) {+ if (pluginState.isAppDownloaded) {emit(state.copyWith(+ runningState: pluginState.state,progressIndicator: const LocalAIProgress.checkPluginState(),),);- } else {- if (state.selectedLLMModel != null) {- // Go to download page if the selected model is downloading- if (llmResource.isDownloading) {- emit(- state.copyWith(- progressIndicator:- LocalAIProgress.startDownloading(state.selectedLLMModel!),- selectLLMState: const ChatLoadingState.finish(),- ),- );- return;- } else {- emit(- state.copyWith(- progressIndicator: LocalAIProgress.showDownload(- llmResource,- state.selectedLLMModel!,- ),- selectLLMState: const ChatLoadingState.finish(),- ),- );- }- }- }- },- startDownloadModel: (LLMModelPB llmModel) {- emit(- state.copyWith(- progressIndicator: LocalAIProgress.startDownloading(llmModel),- selectLLMState: const ChatLoadingState.finish(),- ),- );- },- cancelDownload: () async {- final _ = await AIEventCancelDownloadLLMResource().send();- _fetchCurremtLLMState();- },- finishDownload: () async {- emit(- state.copyWith(- progressIndicator: const LocalAIProgress.finishDownload(),- ),- );- },- updatePluginState: (LocalAIPluginStatePB pluginState) {- if (pluginState.offlineAiReady) {- AIEventRefreshLocalAIModelInfo().send().then((result) {- if (!isClosed) {- add(LocalAIChatSettingEvent.didLoadModelInfo(result));- }- });-- if (pluginState.state == RunningStatePB.Stopped) {- emit(- state.copyWith(- runningState: pluginState.state,- progressIndicator: const LocalAIProgress.checkPluginState(),- ),- );- } else {- emit(- state.copyWith(- runningState: pluginState.state,- ),- );- }} else {emit(state.copyWith(- progressIndicator: const LocalAIProgress.startOfflineAIApp(),+ progressIndicator: const LocalAIProgress.downloadLocalAIApp(),),);}@@ -187,39 +63,6 @@ class LocalAIChatSettingBloc);}- void _fetchCurremtLLMState() async {- final result = await AIEventGetLocalLLMState().send();- result.fold(- (llmResource) {- if (!isClosed) {- add(LocalAIChatSettingEvent.refreshLLMState(llmResource));- }- },- (err) {- Log.error(err);- },- );- }-- /// Handles the event to fetch local AI settings when the application starts.- Future<void> _handleStarted() async {- final result = await AIEventGetLocalAIPluginState().send();- result.fold(- (pluginState) async {- if (!isClosed) {- add(LocalAIChatSettingEvent.updatePluginState(pluginState));- if (pluginState.offlineAiReady) {- final result = await AIEventRefreshLocalAIModelInfo().send();- if (!isClosed) {- add(LocalAIChatSettingEvent.didLoadModelInfo(result));- }- }- }- },- (err) => Log.error(err.toString()),- );- }-@overrideFuture<void> close() async {await listener.stop();@@ -228,60 +71,24 @@ class LocalAIChatSettingBloc}@freezed-class LocalAIChatSettingEvent with _$LocalAIChatSettingEvent {- const factory LocalAIChatSettingEvent.refreshAISetting() = _RefreshAISetting;- const factory LocalAIChatSettingEvent.didLoadModelInfo(- FlowyResult<LLMModelInfoPB, FlowyError> result,- ) = _ModelInfo;- const factory LocalAIChatSettingEvent.selectLLMConfig(LLMModelPB config) =- _SelectLLMConfig;-- const factory LocalAIChatSettingEvent.refreshLLMState(- LocalModelResourcePB llmResource,- ) = _RefreshLLMResource;- const factory LocalAIChatSettingEvent.startDownloadModel(- LLMModelPB llmModel,- ) = _StartDownloadModel;-- const factory LocalAIChatSettingEvent.cancelDownload() = _CancelDownload;- const factory LocalAIChatSettingEvent.finishDownload() = _FinishDownload;- const factory LocalAIChatSettingEvent.updatePluginState(- LocalAIPluginStatePB pluginState,- ) = _PluginState;+class LocalAISettingPanelEvent with _$LocalAISettingPanelEvent {+ const factory LocalAISettingPanelEvent.started() = _Started;+ const factory LocalAISettingPanelEvent.updateAIState(+ LocalAIPB aiState,+ ) = _UpdateAIState;}@freezed-class LocalAIChatSettingState with _$LocalAIChatSettingState {- const factory LocalAIChatSettingState({- LLMModelInfoPB? modelInfo,- LLMModelPB? selectedLLMModel,+class LocalAISettingPanelState with _$LocalAISettingPanelState {+ const factory LocalAISettingPanelState({LocalAIProgress? progressIndicator,- @Default(AIModelProgress.init()) AIModelProgress aiModelProgress,- @Default(ChatLoadingState.loading()) ChatLoadingState selectLLMState,- @Default([]) List<LLMModelPB> models,@Default(RunningStatePB.Connecting) RunningStatePB runningState,}) = _LocalAIChatSettingState;}@freezedclass LocalAIProgress with _$LocalAIProgress {- // when user comes back to the setting page, it will auto detect current llm state- const factory LocalAIProgress.showDownload(- LocalModelResourcePB llmResource,- LLMModelPB llmModel,- ) = _DownloadNeeded;-- // when start downloading the model- const factory LocalAIProgress.startDownloading(LLMModelPB llmModel) =- _Downloading;- const factory LocalAIProgress.finishDownload() = _Finish;- const factory LocalAIProgress.checkPluginState() = _CheckPluginState;- const factory LocalAIProgress.startOfflineAIApp() = _StartOfflineAIApp;-}--@freezed-class AIModelProgress with _$AIModelProgress {- const factory AIModelProgress.init() = _AIModelProgressInit;- const factory AIModelProgress.loading() = _AIModelDownloading;- const factory AIModelProgress.finish({FlowyError? error}) = _AIModelFinish;+ const factory LocalAIProgress.checkPluginState() = _CheckPluginStateProgress;+ const factory LocalAIProgress.downloadLocalAIApp() =+ _DownloadLocalAIAppProgress;}diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_chat_toggle_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_chat_toggle_bloc.dartindex 4feac1247a..f533778f9b 100644--- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_chat_toggle_bloc.dart+++ b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_chat_toggle_bloc.dart@@ -20,25 +20,9 @@ class LocalAIChatToggleBloc) async {await event.when(started: () async {- final result = await AIEventGetLocalAIChatState().send();+ final result = await AIEventGetLocalAIState().send();_handleResult(emit, result);},- toggle: () async {- emit(- state.copyWith(- pageIndicator: const LocalAIChatToggleStateIndicator.loading(),- ),- );- unawaited(- AIEventToggleLocalAIChat().send().then(- (result) {- if (!isClosed) {- add(LocalAIChatToggleEvent.handleResult(result));- }- },- ),- );- },handleResult: (result) {_handleResult(emit, result);},@@ -47,7 +31,7 @@ class LocalAIChatToggleBlocvoid _handleResult(Emitter<LocalAIChatToggleState> emit,- FlowyResult<LocalAIChatPB, FlowyError> result,+ FlowyResult<LocalAIPB, FlowyError> result,) {result.fold((localAI) {@@ -72,9 +56,8 @@ class LocalAIChatToggleBloc@freezedclass LocalAIChatToggleEvent with _$LocalAIChatToggleEvent {const factory LocalAIChatToggleEvent.started() = _Started;- const factory LocalAIChatToggleEvent.toggle() = _Toggle;const factory LocalAIChatToggleEvent.handleResult(- FlowyResult<LocalAIChatPB, FlowyError> result,+ FlowyResult<LocalAIPB, FlowyError> result,) = _HandleResult;}diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_llm_listener.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_llm_listener.dartindex a7778d7d99..40cdb564b2 100644--- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_llm_listener.dart+++ b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_llm_listener.dart@@ -8,8 +8,8 @@ import 'package:appflowy_backend/protobuf/flowy-notification/subject.pb.dart';import 'package:appflowy_backend/rust_stream.dart';import 'package:appflowy_result/appflowy_result.dart';-typedef PluginStateCallback = void Function(LocalAIPluginStatePB state);-typedef LocalAIChatCallback = void Function(LocalAIChatPB chatState);+typedef PluginStateCallback = void Function(LocalAIPB state);+typedef PluginResourceCallback = void Function(LackOfAIResourcePB data);class LocalLLMListener {LocalLLMListener() {@@ -24,15 +24,14 @@ class LocalLLMListener {ChatNotificationParser? _parser;PluginStateCallback? stateCallback;- LocalAIChatCallback? chatStateCallback;- void Function()? finishStreamingCallback;+ PluginResourceCallback? resourceCallback;void start({PluginStateCallback? stateCallback,- LocalAIChatCallback? chatStateCallback,+ PluginResourceCallback? resourceCallback,}) {this.stateCallback = stateCallback;- this.chatStateCallback = chatStateCallback;+ this.resourceCallback = resourceCallback;}void _callback(@@ -41,11 +40,11 @@ class LocalLLMListener {) {result.map((r) {switch (ty) {- case ChatNotification.UpdateChatPluginState:- stateCallback?.call(LocalAIPluginStatePB.fromBuffer(r));+ case ChatNotification.UpdateLocalAIState:+ stateCallback?.call(LocalAIPB.fromBuffer(r));break;- case ChatNotification.UpdateLocalChatAI:- chatStateCallback?.call(LocalAIChatPB.fromBuffer(r));+ case ChatNotification.LocalAIResourceUpdated:+ resourceCallback?.call(LackOfAIResourcePB.fromBuffer(r));break;default:break;diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/ollama_setting_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/ollama_setting_bloc.dartnew file mode 100644index 0000000000..6c9ebe2a3f--- /dev/null+++ b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/ollama_setting_bloc.dart@@ -0,0 +1,221 @@+import 'dart:async';++import 'package:appflowy_backend/dispatch/dispatch.dart';+import 'package:appflowy_backend/log.dart';+import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart';+import 'package:appflowy_result/appflowy_result.dart';+import 'package:bloc/bloc.dart';+import 'package:collection/collection.dart';+import 'package:freezed_annotation/freezed_annotation.dart';+import 'package:equatable/equatable.dart';+part 'ollama_setting_bloc.freezed.dart';++class OllamaSettingBloc extends Bloc<OllamaSettingEvent, OllamaSettingState> {+ OllamaSettingBloc() : super(const OllamaSettingState()) {+ on<OllamaSettingEvent>(_handleEvent);+ }++ Future<void> _handleEvent(+ OllamaSettingEvent event,+ Emitter<OllamaSettingState> emit,+ ) async {+ event.when(+ started: () {+ AIEventGetLocalAISetting().send().fold(+ (setting) {+ if (!isClosed) {+ add(OllamaSettingEvent.didLoadSetting(setting));+ }+ },+ Log.error,+ );+ },+ didLoadSetting: (setting) => _updateSetting(setting, emit),+ updateSetting: (setting) => _updateSetting(setting, emit),+ onEdit: (content, settingType) {+ final updatedSubmittedItems = state.submittedItems+ .map(+ (item) => item.settingType == settingType+ ? SubmittedItem(+ content: content,+ settingType: item.settingType,+ )+ : item,+ )+ .toList();++ // Convert both lists to maps: {settingType: content}+ final updatedMap = {+ for (final item in updatedSubmittedItems)+ item.settingType: item.content,+ };++ final inputMap = {+ for (final item in state.inputItems) item.settingType: item.content,+ };++ // Compare maps instead of lists+ final isEdited = !const MapEquality<SettingType, String>()+ .equals(updatedMap, inputMap);++ emit(+ state.copyWith(+ submittedItems: updatedSubmittedItems,+ isEdited: isEdited,+ ),+ );+ },+ submit: () {+ final setting = LocalAISettingPB();+ final settingUpdaters = <SettingType, void Function(String)>{+ SettingType.serverUrl: (value) => setting.serverUrl = value,+ SettingType.chatModel: (value) => setting.chatModelName = value,+ SettingType.embeddingModel: (value) =>+ setting.embeddingModelName = value,+ };++ for (final item in state.submittedItems) {+ settingUpdaters[item.settingType]?.call(item.content);+ }+ add(OllamaSettingEvent.updateSetting(setting));+ AIEventUpdateLocalAISetting(setting).send().fold(+ (_) {+ Log.info('AI setting updated successfully');+ },+ (err) => Log.error("update ai setting failed: $err"),+ );+ },+ );+ }++ void _updateSetting(+ LocalAISettingPB setting,+ Emitter<OllamaSettingState> emit,+ ) {+ emit(+ state.copyWith(+ setting: setting,+ inputItems: _createInputItems(setting),+ submittedItems: _createSubmittedItems(setting),+ isEdited: false, // Reset to false when the setting is loaded/updated.+ ),+ );+ }++ List<SettingItem> _createInputItems(LocalAISettingPB setting) => [+ SettingItem(+ content: setting.serverUrl,+ hintText: 'http://localhost:11434',+ settingType: SettingType.serverUrl,+ ),+ SettingItem(+ content: setting.chatModelName,+ hintText: 'llama3.1',+ settingType: SettingType.chatModel,+ ),+ SettingItem(+ content: setting.embeddingModelName,+ hintText: 'nomic-embed-text',+ settingType: SettingType.embeddingModel,+ ),+ ];++ List<SubmittedItem> _createSubmittedItems(LocalAISettingPB setting) => [+ SubmittedItem(+ content: setting.serverUrl,+ settingType: SettingType.serverUrl,+ ),+ SubmittedItem(+ content: setting.chatModelName,+ settingType: SettingType.chatModel,+ ),+ SubmittedItem(+ content: setting.embeddingModelName,+ settingType: SettingType.embeddingModel,+ ),+ ];+}++// Create an enum for setting type.+enum SettingType {+ serverUrl,+ chatModel,+ embeddingModel; // semicolon needed after the enum values++ String get title {+ switch (this) {+ case SettingType.serverUrl:+ return 'Ollama server url';+ case SettingType.chatModel:+ return 'Chat model name';+ case SettingType.embeddingModel:+ return 'Embedding model name';+ }+ }+}++class SettingItem extends Equatable {+ const SettingItem({+ required this.content,+ required this.hintText,+ required this.settingType,+ });+ final String content;+ final String hintText;+ final SettingType settingType;+ @override+ List<Object?> get props => [content, settingType];+}++class SubmittedItem extends Equatable {+ const SubmittedItem({+ required this.content,+ required this.settingType,+ });+ final String content;+ final SettingType settingType;++ @override+ List<Object?> get props => [content, settingType];+}++@freezed+class OllamaSettingEvent with _$OllamaSettingEvent {+ const factory OllamaSettingEvent.started() = _Started;+ const factory OllamaSettingEvent.didLoadSetting(LocalAISettingPB setting) =+ _DidLoadSetting;+ const factory OllamaSettingEvent.updateSetting(LocalAISettingPB setting) =+ _UpdateSetting;+ const factory OllamaSettingEvent.onEdit(+ String content,+ SettingType settingType,+ ) = _OnEdit;+ const factory OllamaSettingEvent.submit() = _OnSubmit;+}++@freezed+class OllamaSettingState with _$OllamaSettingState {+ const factory OllamaSettingState({+ LocalAISettingPB? setting,+ @Default([+ SettingItem(+ content: 'http://localhost:11434',+ hintText: 'http://localhost:11434',+ settingType: SettingType.serverUrl,+ ),+ SettingItem(+ content: 'llama3.1',+ hintText: 'llama3.1',+ settingType: SettingType.chatModel,+ ),+ SettingItem(+ content: 'nomic-embed-text',+ hintText: 'nomic-embed-text',+ settingType: SettingType.embeddingModel,+ ),+ ])+ List<SettingItem> inputItems,+ @Default([]) List<SubmittedItem> submittedItems,+ @Default(false) bool isEdited,+ }) = _PluginStateState;+}diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/plugin_state_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/plugin_state_bloc.dartindex 4f24309bde..93b0417e63 100644--- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/plugin_state_bloc.dart+++ b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/plugin_state_bloc.dart@@ -1,6 +1,5 @@import 'dart:async';-import 'package:appflowy/core/helpers/url_launcher.dart';import 'package:appflowy/workspace/application/settings/ai/local_llm_listener.dart';import 'package:appflowy_backend/dispatch/dispatch.dart';import 'package:appflowy_backend/log.dart';@@ -16,13 +15,18 @@ class PluginStateBloc extends Bloc<PluginStateEvent, PluginStateState> {: listener = LocalLLMListener(),super(const PluginStateState(- action: PluginStateAction.init(),+ action: PluginStateAction.unknown(),),) {listener.start(stateCallback: (pluginState) {if (!isClosed) {- add(PluginStateEvent.updateState(pluginState));+ add(PluginStateEvent.updateLocalAIState(pluginState));+ }+ },+ resourceCallback: (data) {+ if (!isClosed) {+ add(PluginStateEvent.resourceStateChange(data));}},);@@ -44,61 +48,70 @@ class PluginStateBloc extends Bloc<PluginStateEvent, PluginStateState> {) async {await event.when(started: () async {- final result = await AIEventGetLocalAIPluginState().send();+ final result = await AIEventGetLocalAIState().send();result.fold((pluginState) {if (!isClosed) {- add(PluginStateEvent.updateState(pluginState));+ add(PluginStateEvent.updateLocalAIState(pluginState));}},(err) => Log.error(err.toString()),);},- updateState: (LocalAIPluginStatePB pluginState) {+ updateLocalAIState: (LocalAIPB aiState) {// if the offline ai is not started, ask user to start it- if (pluginState.offlineAiReady) {+ if (aiState.isAppDownloaded) {+ if (aiState.hasLackOfResource()) {+ emit(+ PluginStateState(+ action:+ PluginStateAction.lackOfResource(aiState.lackOfResource),+ ),+ );+ return;+ }+// Chech state of the plugin- switch (pluginState.state) {+ switch (aiState.state) {+ case RunningStatePB.ReadyToRun:+ emit(+ const PluginStateState(+ action: PluginStateAction.readToRun(),+ ),+ );+case RunningStatePB.Connecting:emit(const PluginStateState(- action: PluginStateAction.loadingPlugin(),+ action: PluginStateAction.initializingPlugin(),),);case RunningStatePB.Running:- emit(const PluginStateState(action: PluginStateAction.ready()));+ emit(const PluginStateState(action: PluginStateAction.running()));break;- default:+ case RunningStatePB.Stopped:emit(state.copyWith(action: const PluginStateAction.restartPlugin()),);+ default:break;}} else {emit(const PluginStateState(- action: PluginStateAction.startAIOfflineApp(),+ action: PluginStateAction.downloadLocalAIApp(),),);}},restartLocalAI: () async {emit(- const PluginStateState(action: PluginStateAction.loadingPlugin()),- );- unawaited(AIEventRestartLocalAIChat().send());- },- openModelDirectory: () async {- final result = await AIEventGetModelStorageDirectory().send();- result.fold(- (data) {- afLaunchUri(Uri.file(data.filePath));- },- (err) => Log.error(err.toString()),+ const PluginStateState(action: PluginStateAction.restartPlugin()),);+ unawaited(AIEventRestartLocalAI().send());},downloadOfflineAIApp: () async {- final result = await AIEventGetOfflineAIAppLink().send();+ final result = await AIEventGetLocalAIDownloadLink().send();await result.fold((app) async {await launchUrl(Uri.parse(app.link));@@ -106,6 +119,13 @@ class PluginStateBloc extends Bloc<PluginStateEvent, PluginStateState> {(err) {},);},+ resourceStateChange: (data) {+ emit(+ PluginStateState(+ action: PluginStateAction.lackOfResource(data.resourceDesc),+ ),+ );+ },);}}@@ -113,12 +133,12 @@ class PluginStateBloc extends Bloc<PluginStateEvent, PluginStateState> {@freezedclass PluginStateEvent with _$PluginStateEvent {const factory PluginStateEvent.started() = _Started;- const factory PluginStateEvent.updateState(LocalAIPluginStatePB pluginState) =- _UpdatePluginState;+ const factory PluginStateEvent.updateLocalAIState(LocalAIPB aiState) =+ _UpdateLocalAIState;const factory PluginStateEvent.restartLocalAI() = _RestartLocalAI;- const factory PluginStateEvent.openModelDirectory() =- _OpenModelStorageDirectory;const factory PluginStateEvent.downloadOfflineAIApp() = _DownloadOfflineAIApp;+ const factory PluginStateEvent.resourceStateChange(LackOfAIResourcePB data) =+ _ResourceStateChange;}@freezed@@ -130,9 +150,11 @@ class PluginStateState with _$PluginStateState {@freezedclass PluginStateAction with _$PluginStateAction {- const factory PluginStateAction.init() = _Init;- const factory PluginStateAction.loadingPlugin() = _LoadingPlugin;- const factory PluginStateAction.ready() = _Ready;+ const factory PluginStateAction.unknown() = _Unknown;+ const factory PluginStateAction.readToRun() = _ReadyToRun;+ const factory PluginStateAction.initializingPlugin() = _InitializingPlugin;+ const factory PluginStateAction.running() = _PluginRunning;const factory PluginStateAction.restartPlugin() = _RestartPlugin;- const factory PluginStateAction.startAIOfflineApp() = _StartAIOfflineApp;+ const factory PluginStateAction.downloadLocalAIApp() = _DownloadLocalAIApp;+ const factory PluginStateAction.lackOfResource(String desc) = _LackOfResource;}diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/downloading_model.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/downloading_model.dartdeleted file mode 100644index a7ed782aea..0000000000--- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/downloading_model.dart+++ /dev/null@@ -1,119 +0,0 @@-import 'package:appflowy/generated/locale_keys.g.dart';-import 'package:appflowy/workspace/application/settings/ai/download_model_bloc.dart';-import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart';-import 'package:easy_localization/easy_localization.dart';-import 'package:flowy_infra/theme_extension.dart';-import 'package:flowy_infra_ui/style_widget/button.dart';-import 'package:flowy_infra_ui/style_widget/text.dart';-import 'package:flowy_infra_ui/widget/spacing.dart';-import 'package:flutter/material.dart';-import 'package:flutter_bloc/flutter_bloc.dart';-import 'package:percent_indicator/linear_percent_indicator.dart';--class DownloadingIndicator extends StatelessWidget {- const DownloadingIndicator({- required this.llmModel,- required this.onCancel,- required this.onFinish,- super.key,- });- final LLMModelPB llmModel;- final VoidCallback onCancel;- final VoidCallback onFinish;-- @override- Widget build(BuildContext context) {- return BlocProvider(- create: (context) =>- DownloadModelBloc(llmModel)..add(const DownloadModelEvent.started()),- child: BlocListener<DownloadModelBloc, DownloadModelState>(- listener: (context, state) {- if (state.isFinish) {- onFinish();- }- },- child: DecoratedBox(- decoration: BoxDecoration(- color: Theme.of(context).colorScheme.surfaceContainerHighest,- borderRadius: BorderRadius.circular(8),- ),- child: BlocBuilder<DownloadModelBloc, DownloadModelState>(- builder: (context, state) {- return Column(- crossAxisAlignment: CrossAxisAlignment.start,- children: [- DownloadingProgressBar(onCancel: onCancel),- if (state.bigFileDownloadPrompt != null) ...[- const VSpace(2),- Opacity(- opacity: 0.6,- child:- FlowyText(state.bigFileDownloadPrompt!, fontSize: 11),- ),- ],- ],- );- },- ),- ),- ),- );- }-}--class DownloadingProgressBar extends StatelessWidget {- const DownloadingProgressBar({required this.onCancel, super.key});-- final VoidCallback onCancel;-- @override- Widget build(BuildContext context) {- return BlocBuilder<DownloadModelBloc, DownloadModelState>(- builder: (context, state) {- return Column(- crossAxisAlignment: CrossAxisAlignment.start,- children: [- Opacity(- opacity: 0.6,- child: FlowyText(- "${LocaleKeys.settings_aiPage_keys_downloadingModel.tr()}: ${state.object}",- fontSize: 11,- ),- ),- IntrinsicHeight(- child: Row(- children: [- Expanded(- child: LinearPercentIndicator(- lineHeight: 9.0,- percent: state.percent,- padding: EdgeInsets.zero,- progressColor: AFThemeExtension.of(context).success,- backgroundColor:- AFThemeExtension.of(context).progressBarBGColor,- barRadius: const Radius.circular(8),- trailing: FlowyText(- "${(state.percent * 100).toStringAsFixed(0)}%",- fontSize: 11,- color: AFThemeExtension.of(context).success,- ),- ),- ),- const HSpace(12),- FlowyButton(- useIntrinsicWidth: true,- text: FlowyText(- LocaleKeys.button_cancel.tr(),- fontSize: 11,- ),- onTap: onCancel,- ),- ],- ),- ),- ],- );- },- );- }-}diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/init_local_ai.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/init_local_ai.dartindex d924b46825..3f557eff91 100644--- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/init_local_ai.dart+++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/init_local_ai.dart@@ -22,7 +22,7 @@ class InitLocalAIIndicator extends StatelessWidget {),child: Padding(padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 4),- child: BlocBuilder<LocalAIChatSettingBloc, LocalAIChatSettingState>(+ child: BlocBuilder<LocalAISettingPanelBloc, LocalAISettingPanelState>(builder: (context, state) {switch (state.runningState) {case RunningStatePB.Connecting:@@ -31,7 +31,7 @@ class InitLocalAIIndicator extends StatelessWidget {children: [const HSpace(8),FlowyText(- LocaleKeys.settings_aiPage_keys_localAILoading.tr(),+ LocaleKeys.settings_aiPage_keys_localAIInitializing.tr(),fontSize: 11,color: const Color(0xFF1E4620),),diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_chat_setting.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_chat_setting.dartdeleted file mode 100644index 9cb3a17d88..0000000000--- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_chat_setting.dart+++ /dev/null@@ -1,370 +0,0 @@-import 'package:appflowy/generated/flowy_svgs.g.dart';-import 'package:appflowy/workspace/application/settings/ai/local_ai_chat_bloc.dart';-import 'package:appflowy/workspace/application/settings/ai/local_ai_chat_toggle_bloc.dart';-import 'package:appflowy/workspace/presentation/home/menu/sidebar/space/shared_widget.dart';-import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/downloading_model.dart';-import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/init_local_ai.dart';-import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/plugin_state.dart';-import 'package:appflowy/workspace/presentation/widgets/dialogs.dart';-import 'package:appflowy/workspace/presentation/widgets/toggle/toggle.dart';-import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart';-import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';-import 'package:expandable/expandable.dart';-import 'package:flowy_infra_ui/flowy_infra_ui.dart';-import 'package:flutter/material.dart';--import 'package:appflowy/generated/locale_keys.g.dart';-import 'package:appflowy/workspace/presentation/settings/shared/af_dropdown_menu_entry.dart';-import 'package:appflowy/workspace/presentation/settings/shared/settings_dropdown.dart';-import 'package:easy_localization/easy_localization.dart';-import 'package:flutter_bloc/flutter_bloc.dart';--class LocalAIChatSetting extends StatelessWidget {- const LocalAIChatSetting({super.key});-- @override- Widget build(BuildContext context) {- return MultiBlocProvider(- providers: [- BlocProvider(create: (context) => LocalAIChatSettingBloc()),- BlocProvider(- create: (context) => LocalAIChatToggleBloc()- ..add(const LocalAIChatToggleEvent.started()),- ),- ],- 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<LocalAIChatSettingBloc>().add(- const LocalAIChatSettingEvent.refreshAISetting(),- );- },- 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<LocalAIChatSettingBloc, LocalAIChatSettingState>(- builder: (context, state) {- // If the progress indicator is startOfflineAIApp, then don't show the LLM model.- if (state.progressIndicator ==- const LocalAIProgress.startOfflineAIApp()) {- return const SizedBox.shrink();- } else {- return Row(- mainAxisAlignment: MainAxisAlignment.spaceBetween,- children: [- Flexible(- child: FlowyText.medium(- LocaleKeys.settings_aiPage_keys_llmModel.tr(),- fontSize: 14,- ),- ),- const Spacer(),- state.aiModelProgress.when(- init: () => const SizedBox.shrink(),- loading: () {- return const Expanded(- child: Row(- children: [- Spacer(),- CircularProgressIndicator.adaptive(),- ],- ),- );- },- finish: (err) => (err == null)- ? const _SelectLocalModelDropdownMenu()- : const SizedBox.shrink(),- ),- ],- );- }- },- ),- const IntrinsicHeight(child: _LocalAIStateWidget()),- ],- ),- ),- ),- ),- ),- );- }-}--class LocalAIChatSettingHeader extends StatelessWidget {- const LocalAIChatSettingHeader({super.key});-- @override- Widget build(BuildContext context) {- return BlocBuilder<LocalAIChatToggleBloc, LocalAIChatToggleState>(- builder: (context, state) {- return state.pageIndicator.when(- error: (error) {- return const SizedBox.shrink();- },- loading: () {- return Row(- children: [- FlowyText(- LocaleKeys.settings_aiPage_keys_localAIStart.tr(),- ),- const Spacer(),- const CircularProgressIndicator.adaptive(),- const HSpace(8),- ],- );- },- ready: (isEnabled) {- return Row(- children: [- const FlowyText('Enable Local AI Chat'),- const Spacer(),- Toggle(- value: isEnabled,- onChanged: (_) {- context- .read<LocalAIChatToggleBloc>()- .add(const LocalAIChatToggleEvent.toggle());- },- ),- ],- );- },- );- },- );- }-}--class _SelectLocalModelDropdownMenu extends StatelessWidget {- const _SelectLocalModelDropdownMenu();-- @override- Widget build(BuildContext context) {- return BlocBuilder<LocalAIChatSettingBloc, LocalAIChatSettingState>(- builder: (context, state) {- return Flexible(- child: SettingsDropdown<LLMModelPB>(- key: const Key('_SelectLocalModelDropdownMenu'),- onChanged: (model) => context.read<LocalAIChatSettingBloc>().add(- LocalAIChatSettingEvent.selectLLMConfig(model),- ),- selectedOption: state.selectedLLMModel!,- options: state.models- .map(- (llm) => buildDropdownMenuEntry<LLMModelPB>(- context,- value: llm,- label: llm.chatModel,- ),- )- .toList(),- ),- );- },- );- }-}--class _LocalAIStateWidget extends StatelessWidget {- const _LocalAIStateWidget();-- @override- Widget build(BuildContext context) {- return BlocBuilder<LocalAIChatSettingBloc, LocalAIChatSettingState>(- builder: (context, state) {- final error = errorFromState(state);- if (error == null) {- // If the error is null, handle selected llm model.- if (state.progressIndicator != null) {- final child = state.progressIndicator!.when(- showDownload: (- LocalModelResourcePB llmResource,- LLMModelPB llmModel,- ) =>- _ShowDownloadIndicator(- llmResource: llmResource,- llmModel: llmModel,- ),- startDownloading: (llmModel) {- return DownloadingIndicator(- key: UniqueKey(),- llmModel: llmModel,- onFinish: () => context- .read<LocalAIChatSettingBloc>()- .add(const LocalAIChatSettingEvent.finishDownload()),- onCancel: () => context- .read<LocalAIChatSettingBloc>()- .add(const LocalAIChatSettingEvent.cancelDownload()),- );- },- finishDownload: () => const InitLocalAIIndicator(),- checkPluginState: () => const PluginStateIndicator(),- startOfflineAIApp: () => OpenOrDownloadOfflineAIApp(- onRetry: () {- context- .read<LocalAIChatSettingBloc>()- .add(const LocalAIChatSettingEvent.refreshAISetting());- },- ),- );-- return Padding(- padding: const EdgeInsets.only(top: 8),- child: child,- );- } else {- return const SizedBox.shrink();- }- } else {- return Opacity(- opacity: 0.5,- child: Padding(- padding: const EdgeInsets.symmetric(vertical: 6),- child: FlowyText(- error.msg,- maxLines: 10,- ),- ),- );- }- },- );- }-- FlowyError? errorFromState(LocalAIChatSettingState state) {- final err = state.aiModelProgress.when(- loading: () => null,- finish: (err) => err,- init: () {},- );-- if (err == null) {- state.selectLLMState.when(- loading: () => null,- finish: (err) => err,- );- }-- return err;- }-}--void _showDownloadDialog(- BuildContext context,- LocalModelResourcePB llmResource,- LLMModelPB llmModel,-) {- if (llmResource.pendingResources.isEmpty) {- return;- }-- final res = llmResource.pendingResources.first;- String desc = "";- switch (res.resType) {- case PendingResourceTypePB.AIModel:- desc = LocaleKeys.settings_aiPage_keys_downloadLLMPromptDetail.tr(- args: [- llmResource.pendingResources[0].name,- llmResource.pendingResources[0].fileSize,- ],- );- break;- case PendingResourceTypePB.OfflineApp:- desc = LocaleKeys.settings_aiPage_keys_downloadAppFlowyOfflineAI.tr();- break;- }-- showConfirmDialog(- context: context,- style: ConfirmPopupStyle.cancelAndOk,- title: LocaleKeys.settings_aiPage_keys_downloadLLMPrompt.tr(- args: [res.name],- ),- description: desc,- confirmLabel: LocaleKeys.button_confirm.tr(),- onConfirm: () => context.read<LocalAIChatSettingBloc>().add(- LocalAIChatSettingEvent.startDownloadModel(- llmModel,- ),- ),- onCancel: () => context.read<LocalAIChatSettingBloc>().add(- const LocalAIChatSettingEvent.cancelDownload(),- ),- );-}--class _ShowDownloadIndicator extends StatelessWidget {- const _ShowDownloadIndicator({- required this.llmResource,- required this.llmModel,- });- final LocalModelResourcePB llmResource;- final LLMModelPB llmModel;-- @override- Widget build(BuildContext context) {- return BlocBuilder<LocalAIChatSettingBloc, LocalAIChatSettingState>(- builder: (context, state) {- return Row(- children: [- const Spacer(),- IntrinsicWidth(- child: SizedBox(- height: 30,- child: FlowyButton(- text: FlowyText(- LocaleKeys.settings_aiPage_keys_downloadAIModelButton.tr(),- fontSize: 14,- color: const Color(0xFF005483),- ),- leftIcon: const FlowySvg(- FlowySvgs.local_model_download_s,- color: Color(0xFF005483),- ),- onTap: () {- _showDownloadDialog(context, llmResource, llmModel);- },- ),- ),- ),- ],- );- },- );- }-}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.dartindex f9dc6fa4d8..706ecdd1a1 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@@ -3,7 +3,7 @@ import 'package:flutter/material.dart';import 'package:appflowy/generated/locale_keys.g.dart';import 'package:appflowy/workspace/application/settings/ai/local_ai_bloc.dart';import 'package:appflowy/workspace/application/settings/ai/settings_ai_bloc.dart';-import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/local_ai_chat_setting.dart';+import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting_panel.dart';import 'package:appflowy/workspace/presentation/widgets/dialogs.dart';import 'package:appflowy/workspace/presentation/widgets/toggle/toggle.dart';import 'package:easy_localization/easy_localization.dart';@@ -68,7 +68,7 @@ class _LocalAISettingState extends State<LocalAISetting> {child: const Padding(padding:EdgeInsets.symmetric(horizontal: 12, vertical: 6),- child: LocalAIChatSetting(),+ child: LocalAISettingPanel(),),),],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.dartnew file mode 100644index 0000000000..66379d347a--- /dev/null+++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting_panel.dart@@ -0,0 +1,125 @@+import 'package:appflowy/workspace/application/settings/ai/local_ai_chat_bloc.dart';+import 'package:appflowy/workspace/application/settings/ai/local_ai_chat_toggle_bloc.dart';+import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/ollma_setting.dart';+import 'package:expandable/expandable.dart';+import 'package:flowy_infra_ui/widget/spacing.dart';+import 'package:flutter/material.dart';+import 'package:flutter_bloc/flutter_bloc.dart';+import 'plugin_state.dart';++class LocalAISettingPanel extends StatelessWidget {+ const LocalAISettingPanel({super.key});++ @override+ Widget build(BuildContext context) {+ return MultiBlocProvider(+ providers: [+ BlocProvider(create: (context) => LocalAISettingPanelBloc()),+ BlocProvider(+ create: (context) => LocalAIChatToggleBloc()+ ..add(const LocalAIChatToggleEvent.started()),+ ),+ ],+ 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(),+ );+ },+ 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(),+ ],+ );+ }+ },+ ),+ ],+ ),+ ),+ ),+ ),+ ),+ );+ }+}++class _LocalAIStateWidget extends StatelessWidget {+ const _LocalAIStateWidget();++ @override+ Widget build(BuildContext context) {+ return BlocBuilder<LocalAISettingPanelBloc, LocalAISettingPanelState>(+ builder: (context, state) {+ if (state.progressIndicator != null) {+ final child = state.progressIndicator!.when(+ downloadLocalAIApp: () => OpenOrDownloadOfflineAIApp(+ onRetry: () {+ context+ .read<LocalAISettingPanelBloc>()+ .add(const LocalAISettingPanelEvent.started());+ },+ ),+ checkPluginState: () => const PluginStateIndicator(),+ );++ return Padding(+ padding: const EdgeInsets.only(top: 8),+ child: child,+ );+ } else {+ return const SizedBox.shrink();+ }+ },+ );+ }+}diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/ollma_setting.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/ollma_setting.dartnew file mode 100644index 0000000000..45394a9db5--- /dev/null+++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/ollma_setting.dart@@ -0,0 +1,165 @@+import 'package:appflowy/core/helpers/url_launcher.dart';+import 'package:appflowy/generated/locale_keys.g.dart';+import 'package:appflowy/workspace/application/settings/ai/ollama_setting_bloc.dart';+import 'package:easy_localization/easy_localization.dart';+import 'package:flowy_infra/size.dart';+import 'package:flowy_infra_ui/style_widget/button.dart';+import 'package:flowy_infra_ui/style_widget/text.dart';+import 'package:flowy_infra_ui/style_widget/text_field.dart';+import 'package:flowy_infra_ui/widget/flowy_tooltip.dart';+import 'package:flowy_infra_ui/widget/spacing.dart';+import 'package:flutter/gestures.dart';+import 'package:flutter/material.dart';+import 'package:flutter_bloc/flutter_bloc.dart';++class OllamaSettingPage extends StatelessWidget {+ const OllamaSettingPage({super.key});++ @override+ Widget build(BuildContext context) {+ return BlocProvider(+ create: (context) =>+ OllamaSettingBloc()..add(const OllamaSettingEvent.started()),+ child: BlocBuilder<OllamaSettingBloc, OllamaSettingState>(+ buildWhen: (previous, current) =>+ previous.inputItems != current.inputItems ||+ previous.isEdited != current.isEdited,+ builder: (context, state) {+ return Column(+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ _InstallOllamaInstruction(),+ const VSpace(12),+ ListView.separated(+ shrinkWrap: true,+ itemCount: state.inputItems.length,+ separatorBuilder: (_, __) => const VSpace(10),+ itemBuilder: (context, index) {+ final item = state.inputItems[index];+ return _SettingItemWidget(item: item);+ },+ ),+ const VSpace(6),+ _SaveButton(isEdited: state.isEdited),+ ],+ );+ },+ ),+ );+ }+}++class _SettingItemWidget extends StatelessWidget {+ const _SettingItemWidget({required this.item});+ final SettingItem item;++ @override+ Widget build(BuildContext context) {+ return Column(+ key: ValueKey(item.content + item.settingType.title),+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ FlowyText(item.settingType.title),+ const VSpace(8),+ FlowyTextField(+ hintText: item.hintText,+ text: item.content,+ onChanged: (content) {+ context.read<OllamaSettingBloc>().add(+ OllamaSettingEvent.onEdit(content, item.settingType),+ );+ },+ ),+ ],+ );+ }+}++class _SaveButton extends StatelessWidget {+ const _SaveButton({required this.isEdited});++ final bool isEdited;++ @override+ Widget build(BuildContext context) {+ final tooltipMessage = isEdited ? 'Click to apply changes' : 'No changes';+ return SizedBox(+ height: 50,+ child: Row(+ children: [+ const Spacer(),+ SizedBox(+ width: 120,+ child: FlowyTooltip(+ message: tooltipMessage,+ child: Opacity(+ opacity: isEdited ? 1 : 0.5,+ child: FlowyTextButton(+ 'Apply',+ mainAxisAlignment: MainAxisAlignment.center,+ onPressed: isEdited+ ? () {+ context+ .read<OllamaSettingBloc>()+ .add(const OllamaSettingEvent.submit());+ }+ : null,+ ),+ ),+ ),+ ),+ ],+ ),+ );+ }+}++class _InstallOllamaInstruction extends StatelessWidget {+ const _InstallOllamaInstruction();++ @override+ Widget build(BuildContext context) {+ return Column(+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ RichText(+ maxLines: 3,+ textAlign: TextAlign.left,+ text: TextSpan(+ children: <TextSpan>[+ TextSpan(+ text:+ "${LocaleKeys.settings_aiPage_keys_localAISetupInstruction1.tr()} ",+ style: Theme.of(context)+ .textTheme+ .bodySmall!+ .copyWith(height: 1.5),+ ),+ TextSpan(+ text:+ " ${LocaleKeys.settings_aiPage_keys_localAISetupInstruction2.tr()} ",+ style: Theme.of(context).textTheme.bodyMedium!.copyWith(+ fontSize: FontSizes.s14,+ color: Theme.of(context).colorScheme.primary,+ height: 1.5,+ ),+ recognizer: TapGestureRecognizer()+ ..onTap = () => afLaunchUrlString(+ "https://docs.appflowy.io/docs/appflowy/product/appflowy-ai-ollama",+ ),+ ),+ TextSpan(+ text:+ " ${LocaleKeys.settings_aiPage_keys_localAISetupInstruction3.tr()} ",+ style: Theme.of(context)+ .textTheme+ .bodySmall!+ .copyWith(height: 1.5),+ ),+ ],+ ),+ ),+ ],+ );+ }+}diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_state.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_state.dartindex bf601b6184..f1896b145b 100644--- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_state.dart+++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_state.dart@@ -3,6 +3,7 @@ import 'package:appflowy/generated/flowy_svgs.g.dart';import 'package:appflowy/generated/locale_keys.g.dart';import 'package:appflowy/workspace/application/settings/ai/download_offline_ai_app_bloc.dart';import 'package:appflowy/workspace/application/settings/ai/plugin_state_bloc.dart';+import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/init_local_ai.dart';import 'package:easy_localization/easy_localization.dart';import 'package:flowy_infra/size.dart';import 'package:flowy_infra_ui/style_widget/button.dart';@@ -23,17 +24,19 @@ class PluginStateIndicator extends StatelessWidget {child: BlocBuilder<PluginStateBloc, PluginStateState>(builder: (context, state) {return state.action.when(- init: () => const _InitPlugin(),- ready: () => const _LocalAIReadyToUse(),- restartPlugin: () => const _ReloadButton(),- loadingPlugin: () => const _InitPlugin(),- startAIOfflineApp: () => OpenOrDownloadOfflineAIApp(+ unknown: () => const SizedBox.shrink(),+ readToRun: () => const SizedBox.shrink(),+ initializingPlugin: () => const InitLocalAIIndicator(),+ running: () => const _LocalAIRunning(),+ restartPlugin: () => const _RestartPluginButton(),+ downloadLocalAIApp: () => OpenOrDownloadOfflineAIApp(onRetry: () {context.read<PluginStateBloc>().add(const PluginStateEvent.started());},),+ lackOfResource: (desc) => _LackOfResource(desc: desc),);},),@@ -41,26 +44,8 @@ class PluginStateIndicator extends StatelessWidget {}}-class _InitPlugin extends StatelessWidget {- const _InitPlugin();-- @override- Widget build(BuildContext context) {- return Row(- children: [- FlowyText(LocaleKeys.settings_aiPage_keys_localAIStart.tr()),- const Spacer(),- const SizedBox(- height: 20,- child: CircularProgressIndicator.adaptive(),- ),- ],- );- }-}--class _ReloadButton extends StatelessWidget {- const _ReloadButton();+class _RestartPluginButton extends StatelessWidget {+ const _RestartPluginButton();@overrideWidget build(BuildContext context) {@@ -91,8 +76,8 @@ class _ReloadButton extends StatelessWidget {}}-class _LocalAIReadyToUse extends StatelessWidget {- const _LocalAIReadyToUse();+class _LocalAIRunning extends StatelessWidget {+ const _LocalAIRunning();@overrideWidget build(BuildContext context) {@@ -119,7 +104,7 @@ class _LocalAIReadyToUse extends StatelessWidget {const HSpace(6),Flexible(child: FlowyText(- LocaleKeys.settings_aiPage_keys_localAILoaded.tr(),+ LocaleKeys.settings_aiPage_keys_localAIRunning.tr(),fontSize: 11,color: const Color(0xFF1E4620),maxLines: 3,@@ -128,22 +113,6 @@ class _LocalAIReadyToUse extends StatelessWidget {],),),- Padding(- padding: const EdgeInsets.symmetric(horizontal: 6),- child: FlowyButton(- useIntrinsicWidth: true,- text: FlowyText(- LocaleKeys.settings_aiPage_keys_openModelDirectory.tr(),- fontSize: 11,- color: const Color(0xFF1E4620),- ),- onTap: () {- context.read<PluginStateBloc>().add(- const PluginStateEvent.openModelDirectory(),- );- },- ),- ),],),),@@ -232,20 +201,6 @@ class OpenOrDownloadOfflineAIApp extends StatelessWidget {],),),- // const SizedBox(- // height: 6,- // ), // Replaced VSpace with SizedBox for simplicity- // SizedBox(- // height: 30,- // child: FlowyButton(- // useIntrinsicWidth: true,- // margin: const EdgeInsets.symmetric(horizontal: 12),- // text: FlowyText(- // LocaleKeys.settings_aiPage_keys_activeOfflineAI.tr(),- // ),- // onTap: onRetry,- // ),- // ),],);},@@ -253,3 +208,14 @@ class OpenOrDownloadOfflineAIApp extends StatelessWidget {);}}++class _LackOfResource extends StatelessWidget {+ const _LackOfResource({required this.desc});++ final String desc;++ @override+ Widget build(BuildContext context) {+ return FlowyText(desc);+ }+}diff --git a/frontend/resources/translations/en.json b/frontend/resources/translations/en.jsonindex 3a78ef813f..e6a5658865 100644--- a/frontend/resources/translations/en.json+++ b/frontend/resources/translations/en.json@@ -850,6 +850,8 @@"localAIStart": "Local AI Chat is starting...","localAILoading": "Local AI Chat Model is loading...","localAIStopped": "Local AI stopped",+ "localAIRunning": "Local AI is running",+ "localAIInitializing": "Local AI is initializing...","failToLoadLocalAI": "Failed to start local AI","restartLocalAI": "Restart Local AI","disableLocalAITitle": "Disable local AI",@@ -863,7 +865,10 @@"offlineAIDownload3": "it first","activeOfflineAI": "Active","downloadOfflineAI": "Download",- "openModelDirectory": "Open folder"+ "openModelDirectory": "Open folder",+ "localAISetupInstruction1": "Follow the",+ "localAISetupInstruction2": "instruction",+ "localAISetupInstruction3": "to setup your Ollama"}},"planPage": {diff --git a/frontend/rust-lib/Cargo.lock b/frontend/rust-lib/Cargo.lockindex 50cfd1cc7e..ddd730c1c3 100644--- a/frontend/rust-lib/Cargo.lock+++ b/frontend/rust-lib/Cargo.lock@@ -198,11 +198,12 @@ dependencies = [[[package]]name = "appflowy-local-ai"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-LocalAI?rev=6f064efe232268f8d396edbb4b84d57fbb640f13#6f064efe232268f8d396edbb4b84d57fbb640f13"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-LocalAI?rev=99447e23d5563328e5a2c2b0749e44540359a818#99447e23d5563328e5a2c2b0749e44540359a818"dependencies = ["anyhow","appflowy-plugin","bytes",+ "futures","reqwest 0.11.27","serde","serde_json",@@ -217,7 +218,7 @@ dependencies = [[[package]]name = "appflowy-plugin"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-LocalAI?rev=6f064efe232268f8d396edbb4b84d57fbb640f13#6f064efe232268f8d396edbb4b84d57fbb640f13"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-LocalAI?rev=99447e23d5563328e5a2c2b0749e44540359a818#99447e23d5563328e5a2c2b0749e44540359a818"dependencies = ["anyhow","cfg-if",@@ -1395,7 +1396,7 @@ dependencies = ["cssparser-macros","dtoa-short","itoa",- "phf 0.8.0",+ "phf 0.11.2","smallvec",]@@ -2725,9 +2726,9 @@ dependencies = [[[package]]name = "futures"-version = "0.3.30"+version = "0.3.31"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"+checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"dependencies = ["futures-channel","futures-core",@@ -2740,9 +2741,9 @@ dependencies = [[[package]]name = "futures-channel"-version = "0.3.30"+version = "0.3.31"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"+checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"dependencies = ["futures-core","futures-sink",@@ -2750,15 +2751,15 @@ dependencies = [[[package]]name = "futures-core"-version = "0.3.30"+version = "0.3.31"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"+checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"[[package]]name = "futures-executor"-version = "0.3.30"+version = "0.3.31"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"+checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"dependencies = ["futures-core","futures-task",@@ -2767,9 +2768,9 @@ dependencies = [[[package]]name = "futures-io"-version = "0.3.30"+version = "0.3.31"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"+checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"[[package]]name = "futures-lite"@@ -2786,9 +2787,9 @@ dependencies = [[[package]]name = "futures-macro"-version = "0.3.30"+version = "0.3.31"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"+checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"dependencies = ["proc-macro2","quote",@@ -2797,21 +2798,21 @@ dependencies = [[[package]]name = "futures-sink"-version = "0.3.30"+version = "0.3.31"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5"+checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7"[[package]]name = "futures-task"-version = "0.3.30"+version = "0.3.31"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004"+checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988"[[package]]name = "futures-util"-version = "0.3.30"+version = "0.3.31"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"+checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"dependencies = ["futures-channel","futures-core",@@ -4612,7 +4613,7 @@ version = "0.8.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12"dependencies = [- "phf_macros",+ "phf_macros 0.8.0","phf_shared 0.8.0","proc-macro-hack",]@@ -4632,6 +4633,7 @@ version = "0.11.2"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc"dependencies = [+ "phf_macros 0.11.3","phf_shared 0.11.2",]@@ -4699,6 +4701,19 @@ dependencies = ["syn 1.0.109",]+[[package]]+name = "phf_macros"+version = "0.11.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "f84ac04429c13a7ff43785d75ad27569f2951ce0ffd30a3321230db2fc727216"+dependencies = [+ "phf_generator 0.11.2",+ "phf_shared 0.11.2",+ "proc-macro2",+ "quote",+ "syn 2.0.94",+]+[[package]]name = "phf_shared"version = "0.8.0"diff --git a/frontend/rust-lib/Cargo.toml b/frontend/rust-lib/Cargo.tomlindex 27c81a9f96..c73ac79d72 100644--- a/frontend/rust-lib/Cargo.toml+++ b/frontend/rust-lib/Cargo.toml@@ -79,7 +79,7 @@ diesel = { version = "2.1.0", features = [] }uuid = { version = "1.5.0", features = ["serde", "v4", "v5"] }serde_repr = "0.1"-futures = "0.3.29"+futures = "0.3.31"tokio = "1.38.0"tokio-stream = "0.1.14"async-trait = "0.1.81"@@ -152,5 +152,5 @@ collab-importer = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFl# To update the commit ID, run:# scripts/tool/update_local_ai_rev.sh new_rev_id# ⚠️⚠️⚠️️-appflowy-local-ai = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "6f064efe232268f8d396edbb4b84d57fbb640f13" }-appflowy-plugin = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "6f064efe232268f8d396edbb4b84d57fbb640f13" }+appflowy-local-ai = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "99447e23d5563328e5a2c2b0749e44540359a818" }+appflowy-plugin = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "99447e23d5563328e5a2c2b0749e44540359a818" }diff --git a/frontend/rust-lib/collab-integrate/Cargo.toml b/frontend/rust-lib/collab-integrate/Cargo.tomlindex ab375c77fc..b817d639f1 100644--- a/frontend/rust-lib/collab-integrate/Cargo.toml+++ b/frontend/rust-lib/collab-integrate/Cargo.toml@@ -22,7 +22,7 @@ tracing.workspace = trueasync-trait.workspace = truetokio = { workspace = true, features = ["sync"] }lib-infra = { workspace = true }-futures = "0.3"+futures = "0.3.31"arc-swap = "1.7"flowy-sqlite = { workspace = true }diesel.workspace = truediff --git a/frontend/rust-lib/dart-ffi/Cargo.toml b/frontend/rust-lib/dart-ffi/Cargo.tomlindex 8ad17c028f..969f64e6f9 100644--- a/frontend/rust-lib/dart-ffi/Cargo.toml+++ b/frontend/rust-lib/dart-ffi/Cargo.toml@@ -44,7 +44,7 @@ collab-integrate = { workspace = true }flowy-derive.workspace = trueserde_yaml = "0.9.27"flowy-error = { workspace = true, features = ["impl_from_sqlite", "impl_from_dispatch_error", "impl_from_appflowy_cloud", "impl_from_reqwest", "impl_from_serde", "dart"] }-futures = "0.3.26"+futures = "0.3.31"[features]default = ["dart"]diff --git a/frontend/rust-lib/event-integration-test/Cargo.toml b/frontend/rust-lib/event-integration-test/Cargo.tomlindex 392d82d858..21d94ae28a 100644--- a/frontend/rust-lib/event-integration-test/Cargo.toml+++ b/frontend/rust-lib/event-integration-test/Cargo.toml@@ -55,7 +55,7 @@ tokio-postgres = { version = "0.7.8" }chrono = "0.4.31"zip.workspace = truewalkdir = "2.5.0"-futures = "0.3.30"+futures = "0.3.31"flowy-ai-pub = { workspace = true }[features]diff --git a/frontend/rust-lib/flowy-ai/src/ai_manager.rs b/frontend/rust-lib/flowy-ai/src/ai_manager.rsindex 5a784f6db6..7850d8767b 100644--- a/frontend/rust-lib/flowy-ai/src/ai_manager.rs+++ b/frontend/rust-lib/flowy-ai/src/ai_manager.rs@@ -3,7 +3,7 @@ use crate::entities::{ChatInfoPB, ChatMessageListPB, ChatMessagePB, ChatSettingsPB, FilePB, PredefinedFormatPB,RepeatedRelatedQuestionPB, StreamMessageParams,};-use crate::local_ai::local_llm_chat::LocalAIController;+use crate::local_ai::controller::LocalAIController;use crate::middleware::chat_service_mw::AICloudServiceMiddleware;use crate::persistence::{insert_chat, read_chat_metadata, ChatTable};use std::collections::HashMap;@@ -58,7 +58,7 @@ pub struct AIManager {pub user_service: Arc<dyn AIUserService>,pub external_service: Arc<dyn AIExternalService>,chats: Arc<DashMap<String, Arc<Chat>>>,- pub local_ai_controller: Arc<LocalAIController>,+ pub local_ai: Arc<LocalAIController>,store_preferences: Arc<KVStorePreferences>,}@@ -72,19 +72,23 @@ impl AIManager {) -> AIManager {let user_service = Arc::new(user_service);let plugin_manager = Arc::new(PluginManager::new());- let local_ai_controller = Arc::new(LocalAIController::new(+ let local_ai = Arc::new(LocalAIController::new(plugin_manager.clone(),store_preferences.clone(),user_service.clone(),chat_cloud_service.clone(),));- let external_service = Arc::new(query_service);- // setup local chat service+ let cloned_local_ai = local_ai.clone();+ tokio::task::spawn_blocking(move || {+ futures::executor::block_on(cloned_local_ai.init_plugin_when_first_run());+ });++ let external_service = Arc::new(query_service);let cloud_service_wm = Arc::new(AICloudServiceMiddleware::new(user_service.clone(),chat_cloud_service,- local_ai_controller.clone(),+ local_ai.clone(),storage_service,));@@ -92,15 +96,14 @@ impl AIManager {cloud_service_wm,user_service,chats: Arc::new(DashMap::new()),- local_ai_controller,+ local_ai,external_service,store_preferences,}}pub async fn initialize(&self, _workspace_id: &str) -> Result<(), FlowyError> {- // Ignore following error- let _ = self.local_ai_controller.refresh().await;+ let _ = self.local_ai.reload().await;Ok(())}@@ -113,9 +116,9 @@ impl AIManager {self.cloud_service_wm.clone(),))});- trace!("[AI Plugin] notify open chat: {}", chat_id);- if self.local_ai_controller.is_running() {- self.local_ai_controller.open_chat(chat_id);+ if self.local_ai.is_running() {+ trace!("[AI Plugin] notify open chat: {}", chat_id);+ self.local_ai.open_chat(chat_id);}let user_service = self.user_service.clone();@@ -146,7 +149,7 @@ impl AIManager {pub async fn close_chat(&self, chat_id: &str) -> Result<(), FlowyError> {trace!("close chat: {}", chat_id);- self.local_ai_controller.close_chat(chat_id);+ self.local_ai.close_chat(chat_id);Ok(())}@@ -154,9 +157,9 @@ impl AIManager {if let Some((_, chat)) = self.chats.remove(chat_id) {chat.close();- if self.local_ai_controller.is_running() {+ if self.local_ai.is_running() {info!("[AI Plugin] notify close chat: {}", chat_id);- self.local_ai_controller.close_chat(chat_id);+ self.local_ai.close_chat(chat_id);}}Ok(())diff --git a/frontend/rust-lib/flowy-ai/src/entities.rs b/frontend/rust-lib/flowy-ai/src/entities.rsindex 7b1cb66593..f86cf4c743 100644--- a/frontend/rust-lib/flowy-ai/src/entities.rs+++ b/frontend/rust-lib/flowy-ai/src/entities.rs@@ -1,8 +1,8 @@-use crate::local_ai::local_llm_chat::LLMModelInfo;use appflowy_plugin::core::plugin::RunningState;use std::collections::HashMap;-use crate::local_ai::local_llm_resource::PendingResource;+use crate::local_ai::controller::LocalAISetting;+use crate::local_ai::resource::PendingResource;use flowy_ai_pub::cloud::{ChatMessage, ChatMessageMetadata, ChatMessageType, LLMModel, OutputContent, OutputLayout,RelatedQuestion, RepeatedChatMessage, RepeatedRelatedQuestion, ResponseFormat,@@ -309,24 +309,6 @@ impl From<RepeatedRelatedQuestion> for RepeatedRelatedQuestionPB {}}-#[derive(Debug, Clone, Default, ProtoBuf)]-pub struct LLMModelInfoPB {- #[pb(index = 1)]- pub selected_model: LLMModelPB,-- #[pb(index = 2)]- pub models: Vec<LLMModelPB>,-}--impl From<LLMModelInfo> for LLMModelInfoPB {- fn from(value: LLMModelInfo) -> Self {- LLMModelInfoPB {- selected_model: LLMModelPB::from(value.selected_model),- models: value.models.into_iter().map(LLMModelPB::from).collect(),- }- }-}-#[derive(Debug, Clone, Default, ProtoBuf)]pub struct LLMModelPB {#[pb(index = 1)]@@ -423,17 +405,6 @@ pub struct ChatFilePB {pub chat_id: String,}-#[derive(Default, ProtoBuf, Clone, Debug)]-pub struct DownloadLLMPB {- #[pb(index = 1)]- pub progress_stream: i64,-}--#[derive(Default, ProtoBuf, Clone, Debug)]-pub struct DownloadTaskPB {- #[pb(index = 1)]- pub task_id: String,-}#[derive(Default, ProtoBuf, Clone, Debug)]pub struct LocalModelStatePB {#[pb(index = 1)]@@ -452,18 +423,6 @@ pub struct LocalModelStatePB {pub is_downloading: bool,}-#[derive(Default, ProtoBuf, Clone, Debug)]-pub struct LocalModelResourcePB {- #[pb(index = 1)]- pub is_ready: bool,-- #[pb(index = 2)]- pub pending_resources: Vec<PendingResourcePB>,-- #[pb(index = 3)]- pub is_downloading: bool,-}-#[derive(Default, ProtoBuf, Clone, Debug)]pub struct PendingResourcePB {#[pb(index = 1)]@@ -482,40 +441,33 @@ pub struct PendingResourcePB {#[derive(Debug, Default, Clone, ProtoBuf_Enum, PartialEq, Eq, Copy)]pub enum PendingResourceTypePB {#[default]- OfflineApp = 0,+ LocalAIAppRes = 0,AIModel = 1,}impl From<PendingResource> for PendingResourceTypePB {fn from(value: PendingResource) -> Self {match value {- PendingResource::OfflineApp { .. } => PendingResourceTypePB::OfflineApp,- PendingResource::ModelInfoRes { .. } => PendingResourceTypePB::AIModel,+ PendingResource::LocalAIAppNotDownloaded { .. } => PendingResourceTypePB::LocalAIAppRes,+ _ => PendingResourceTypePB::AIModel,}}}-#[derive(Default, ProtoBuf, Clone, Debug)]-pub struct LocalAIPluginStatePB {- #[pb(index = 1)]- pub state: RunningStatePB,-- #[pb(index = 2)]- pub offline_ai_ready: bool,-}-#[derive(Debug, Default, Clone, ProtoBuf_Enum, PartialEq, Eq, Copy)]pub enum RunningStatePB {#[default]- Connecting = 0,- Connected = 1,- Running = 2,- Stopped = 3,+ ReadyToRun = 0,+ Connecting = 1,+ Connected = 2,+ Running = 3,+ Stopped = 4,}impl From<RunningState> for RunningStatePB {fn from(value: RunningState) -> Self {match value {+ RunningState::ReadyToConnect => RunningStatePB::ReadyToRun,RunningState::Connecting => RunningStatePB::Connecting,RunningState::Connected { .. } => RunningStatePB::Connected,RunningState::Running { .. } => RunningStatePB::Running,@@ -529,28 +481,19 @@ impl From<RunningState> for RunningStatePB {pub struct LocalAIPB {#[pb(index = 1)]pub enabled: bool,-}--#[derive(Default, ProtoBuf, Clone, Debug)]-pub struct LocalAIChatPB {- #[pb(index = 1)]- pub enabled: bool,#[pb(index = 2)]- pub file_enabled: bool,+ pub is_app_downloaded: bool,- #[pb(index = 3)]- pub plugin_state: LocalAIPluginStatePB,-}+ #[pb(index = 3, one_of)]+ pub lack_of_resource: Option<String>,-#[derive(Default, ProtoBuf, Clone, Debug)]-pub struct LocalModelStoragePB {- #[pb(index = 1)]- pub file_path: String,+ #[pb(index = 4)]+ pub state: RunningStatePB,}#[derive(Default, ProtoBuf, Clone, Debug)]-pub struct OfflineAIPB {+pub struct LocalAIAppLinkPB {#[pb(index = 1)]pub link: String,}@@ -636,3 +579,44 @@ impl From<PredefinedFormatPB> for ResponseFormat {}}}++#[derive(Default, ProtoBuf, Validate, Clone, Debug)]+pub struct LocalAISettingPB {+ #[pb(index = 1)]+ #[validate(custom(function = "required_not_empty_str"))]+ pub server_url: String,++ #[pb(index = 2)]+ #[validate(custom(function = "required_not_empty_str"))]+ pub chat_model_name: String,++ #[pb(index = 3)]+ #[validate(custom(function = "required_not_empty_str"))]+ pub embedding_model_name: String,+}++impl From<LocalAISetting> for LocalAISettingPB {+ fn from(value: LocalAISetting) -> Self {+ LocalAISettingPB {+ server_url: value.ollama_server_url,+ chat_model_name: value.chat_model_name,+ embedding_model_name: value.embedding_model_name,+ }+ }+}++impl From<LocalAISettingPB> for LocalAISetting {+ fn from(value: LocalAISettingPB) -> Self {+ LocalAISetting {+ ollama_server_url: value.server_url,+ chat_model_name: value.chat_model_name,+ embedding_model_name: value.embedding_model_name,+ }+ }+}++#[derive(Default, ProtoBuf, Clone, Debug)]+pub struct LackOfAIResourcePB {+ #[pb(index = 1)]+ pub resource_desc: String,+}diff --git a/frontend/rust-lib/flowy-ai/src/event_handler.rs b/frontend/rust-lib/flowy-ai/src/event_handler.rsindex 746a843da5..5a8018ae4b 100644--- a/frontend/rust-lib/flowy-ai/src/event_handler.rs+++ b/frontend/rust-lib/flowy-ai/src/event_handler.rs@@ -4,15 +4,9 @@ use std::path::PathBuf;use crate::ai_manager::AIManager;use crate::completion::AICompletion;use crate::entities::*;-use crate::local_ai::local_llm_chat::LLMModelInfo;-use crate::notification::{- chat_notification_builder, ChatNotification, APPFLOWY_AI_NOTIFICATION_KEY,-};-use allo_isolate::Isolate;use flowy_ai_pub::cloud::{ChatMessageMetadata, ChatMessageType, ChatRAGData, ContextLoader};use flowy_error::{ErrorCode, FlowyError, FlowyResult};use lib_dispatch::prelude::{data_result_ok, AFPluginData, AFPluginState, DataResult};-use lib_infra::isolate_stream::IsolateSink;use serde_json::json;use std::sync::{Arc, Weak};use tracing::trace;@@ -109,7 +103,7 @@ pub(crate) async fn regenerate_response_handler(}#[tracing::instrument(level = "debug", skip_all, err)]-pub(crate) async fn get_available_model_list_handler(+pub(crate) async fn get_model_list_handler(ai_manager: AFPluginState<Weak<AIManager>>,) -> DataResult<ModelConfigPB, FlowyError> {let ai_manager = upgrade_ai_manager(ai_manager)?;@@ -193,47 +187,6 @@ pub(crate) async fn stop_stream_handler(Ok(())}-#[tracing::instrument(level = "debug", skip_all, err)]-pub(crate) async fn refresh_local_ai_info_handler(- ai_manager: AFPluginState<Weak<AIManager>>,-) -> DataResult<LLMModelInfoPB, FlowyError> {- let ai_manager = upgrade_ai_manager(ai_manager)?;- let model_info = ai_manager.local_ai_controller.refresh_model_info().await;- if model_info.is_err() {- if let Some(llm_model) = ai_manager.local_ai_controller.get_current_model() {- let model_info = LLMModelInfo {- selected_model: llm_model.clone(),- models: vec![llm_model],- };- return data_result_ok(model_info.into());- }- }- data_result_ok(model_info?.into())-}--#[tracing::instrument(level = "debug", skip_all, err)]-pub(crate) async fn update_local_llm_model_handler(- data: AFPluginData<LLMModelPB>,- ai_manager: AFPluginState<Weak<AIManager>>,-) -> DataResult<LocalModelResourcePB, FlowyError> {- let data = data.into_inner();- let ai_manager = upgrade_ai_manager(ai_manager)?;- let state = ai_manager- .local_ai_controller- .select_local_llm(data.llm_id)- .await?;- data_result_ok(state)-}--#[tracing::instrument(level = "debug", skip_all, err)]-pub(crate) async fn get_local_llm_state_handler(- ai_manager: AFPluginState<Weak<AIManager>>,-) -> DataResult<LocalModelResourcePB, FlowyError> {- let ai_manager = upgrade_ai_manager(ai_manager)?;- let state = ai_manager.local_ai_controller.get_local_llm_state().await?;- data_result_ok(state)-}-pub(crate) async fn start_complete_text_handler(data: AFPluginData<CompleteTextPB>,tools: AFPluginState<Arc<AICompletion>>,@@ -301,108 +254,11 @@ pub(crate) async fn chat_file_handler(}#[tracing::instrument(level = "debug", skip_all, err)]-pub(crate) async fn download_llm_resource_handler(- data: AFPluginData<DownloadLLMPB>,- ai_manager: AFPluginState<Weak<AIManager>>,-) -> DataResult<DownloadTaskPB, FlowyError> {- let data = data.into_inner();- let ai_manager = upgrade_ai_manager(ai_manager)?;- let text_sink = IsolateSink::new(Isolate::new(data.progress_stream));- let task_id = ai_manager- .local_ai_controller- .start_downloading(text_sink)- .await?;- data_result_ok(DownloadTaskPB { task_id })-}--#[tracing::instrument(level = "debug", skip_all, err)]-pub(crate) async fn cancel_download_llm_resource_handler(+pub(crate) async fn restart_local_ai_handler(ai_manager: AFPluginState<Weak<AIManager>>,) -> Result<(), FlowyError> {let ai_manager = upgrade_ai_manager(ai_manager)?;- ai_manager.local_ai_controller.cancel_download()?;- Ok(())-}--#[tracing::instrument(level = "debug", skip_all, err)]-pub(crate) async fn get_plugin_state_handler(- ai_manager: AFPluginState<Weak<AIManager>>,-) -> DataResult<LocalAIPluginStatePB, FlowyError> {- let ai_manager = upgrade_ai_manager(ai_manager)?;- let state = ai_manager.local_ai_controller.get_chat_plugin_state();- data_result_ok(state)-}-#[tracing::instrument(level = "debug", skip_all, err)]-pub(crate) async fn toggle_local_ai_chat_handler(- ai_manager: AFPluginState<Weak<AIManager>>,-) -> DataResult<LocalAIChatPB, FlowyError> {- let ai_manager = upgrade_ai_manager(ai_manager)?;- let enabled = ai_manager- .local_ai_controller- .toggle_local_ai_chat()- .await?;- let file_enabled = ai_manager.local_ai_controller.is_rag_enabled();- let plugin_state = ai_manager.local_ai_controller.get_chat_plugin_state();- let pb = LocalAIChatPB {- enabled,- file_enabled,- plugin_state,- };- chat_notification_builder(- APPFLOWY_AI_NOTIFICATION_KEY,- ChatNotification::UpdateLocalChatAI,- )- .payload(pb.clone())- .send();- data_result_ok(pb)-}--#[tracing::instrument(level = "debug", skip_all, err)]-pub(crate) async fn toggle_local_ai_chat_file_handler(- ai_manager: AFPluginState<Weak<AIManager>>,-) -> DataResult<LocalAIChatPB, FlowyError> {- let ai_manager = upgrade_ai_manager(ai_manager)?;- let enabled = ai_manager.local_ai_controller.is_chat_enabled();- let file_enabled = ai_manager- .local_ai_controller- .toggle_local_ai_chat_rag()- .await?;- let plugin_state = ai_manager.local_ai_controller.get_chat_plugin_state();- let pb = LocalAIChatPB {- enabled,- file_enabled,- plugin_state,- };- chat_notification_builder(- APPFLOWY_AI_NOTIFICATION_KEY,- ChatNotification::UpdateLocalChatAI,- )- .payload(pb.clone())- .send();-- data_result_ok(pb)-}--#[tracing::instrument(level = "debug", skip_all, err)]-pub(crate) async fn get_local_ai_chat_state_handler(- ai_manager: AFPluginState<Weak<AIManager>>,-) -> DataResult<LocalAIChatPB, FlowyError> {- let ai_manager = upgrade_ai_manager(ai_manager)?;- let enabled = ai_manager.local_ai_controller.is_chat_enabled();- let file_enabled = ai_manager.local_ai_controller.is_rag_enabled();- let plugin_state = ai_manager.local_ai_controller.get_chat_plugin_state();- data_result_ok(LocalAIChatPB {- enabled,- file_enabled,- plugin_state,- })-}-#[tracing::instrument(level = "debug", skip_all, err)]-pub(crate) async fn restart_local_ai_chat_handler(- ai_manager: AFPluginState<Weak<AIManager>>,-) -> Result<(), FlowyError> {- let ai_manager = upgrade_ai_manager(ai_manager)?;- ai_manager.local_ai_controller.restart_chat_plugin();+ ai_manager.local_ai.restart_plugin().await;Ok(())}@@ -411,8 +267,9 @@ pub(crate) async fn toggle_local_ai_handler(ai_manager: AFPluginState<Weak<AIManager>>,) -> DataResult<LocalAIPB, FlowyError> {let ai_manager = upgrade_ai_manager(ai_manager)?;- let enabled = ai_manager.local_ai_controller.toggle_local_ai().await?;- data_result_ok(LocalAIPB { enabled })+ let _ = ai_manager.local_ai.toggle_local_ai().await?;+ let state = ai_manager.local_ai.get_local_ai_state().await;+ data_result_ok(state)}#[tracing::instrument(level = "debug", skip_all, err)]@@ -420,31 +277,17 @@ pub(crate) async fn get_local_ai_state_handler(ai_manager: AFPluginState<Weak<AIManager>>,) -> DataResult<LocalAIPB, FlowyError> {let ai_manager = upgrade_ai_manager(ai_manager)?;- let enabled = ai_manager.local_ai_controller.is_enabled();- data_result_ok(LocalAIPB { enabled })-}--#[tracing::instrument(level = "debug", skip_all, err)]-pub(crate) async fn get_model_storage_directory_handler(- ai_manager: AFPluginState<Weak<AIManager>>,-) -> DataResult<LocalModelStoragePB, FlowyError> {- let ai_manager = upgrade_ai_manager(ai_manager)?;- let file_path = ai_manager- .local_ai_controller- .get_model_storage_directory()?;- data_result_ok(LocalModelStoragePB { file_path })+ let state = ai_manager.local_ai.get_local_ai_state().await;+ data_result_ok(state)}#[tracing::instrument(level = "debug", skip_all, err)]pub(crate) async fn get_offline_app_handler(ai_manager: AFPluginState<Weak<AIManager>>,-) -> DataResult<OfflineAIPB, FlowyError> {+) -> DataResult<LocalAIAppLinkPB, FlowyError> {let ai_manager = upgrade_ai_manager(ai_manager)?;- let link = ai_manager- .local_ai_controller- .get_offline_ai_app_download_link()- .await?;- data_result_ok(OfflineAIPB { link })+ let link = ai_manager.local_ai.get_plugin_download_link().await?;+ data_result_ok(LocalAIAppLinkPB { link })}#[tracing::instrument(level = "debug", skip_all, err)]@@ -493,3 +336,27 @@ pub(crate) async fn update_chat_settings_handler(Ok(())}++#[tracing::instrument(level = "debug", skip_all, err)]+pub(crate) async fn get_local_ai_setting_handler(+ ai_manager: AFPluginState<Weak<AIManager>>,+) -> DataResult<LocalAISettingPB, FlowyError> {+ let ai_manager = upgrade_ai_manager(ai_manager)?;+ let setting = ai_manager.local_ai.get_local_ai_setting();+ let pb = LocalAISettingPB::from(setting);+ data_result_ok(pb)+}++#[tracing::instrument(level = "debug", skip_all, err)]+pub(crate) async fn update_local_ai_setting_handler(+ ai_manager: AFPluginState<Weak<AIManager>>,+ data: AFPluginData<LocalAISettingPB>,+) -> Result<(), FlowyError> {+ let data = data.try_into_inner()?;+ let ai_manager = upgrade_ai_manager(ai_manager)?;+ ai_manager+ .local_ai+ .update_local_ai_setting(data.into())+ .await?;+ Ok(())+}diff --git a/frontend/rust-lib/flowy-ai/src/event_map.rs b/frontend/rust-lib/flowy-ai/src/event_map.rsindex 2bd2bee863..7875e7c3c9 100644--- a/frontend/rust-lib/flowy-ai/src/event_map.rs+++ b/frontend/rust-lib/flowy-ai/src/event_map.rs@@ -23,47 +23,24 @@ pub fn init(ai_manager: Weak<AIManager>) -> AFPlugin {.event(AIEvent::GetRelatedQuestion, get_related_question_handler).event(AIEvent::GetAnswerForQuestion, get_answer_handler).event(AIEvent::StopStream, stop_stream_handler)- .event(- AIEvent::RefreshLocalAIModelInfo,- refresh_local_ai_info_handler,- )- .event(AIEvent::UpdateLocalLLM, update_local_llm_model_handler)- .event(AIEvent::GetLocalLLMState, get_local_llm_state_handler).event(AIEvent::CompleteText, start_complete_text_handler).event(AIEvent::StopCompleteText, stop_complete_text_handler).event(AIEvent::ChatWithFile, chat_file_handler)- .event(AIEvent::DownloadLLMResource, download_llm_resource_handler)- .event(- AIEvent::CancelDownloadLLMResource,- cancel_download_llm_resource_handler,- )- .event(AIEvent::GetLocalAIPluginState, get_plugin_state_handler)- .event(AIEvent::ToggleLocalAIChat, toggle_local_ai_chat_handler)- .event(- AIEvent::GetLocalAIChatState,- get_local_ai_chat_state_handler,- )- .event(AIEvent::RestartLocalAIChat, restart_local_ai_chat_handler)+ .event(AIEvent::RestartLocalAI, restart_local_ai_handler).event(AIEvent::ToggleLocalAI, toggle_local_ai_handler).event(AIEvent::GetLocalAIState, get_local_ai_state_handler)+ .event(AIEvent::GetLocalAIDownloadLink, get_offline_app_handler)+ .event(AIEvent::GetLocalAISetting, get_local_ai_setting_handler).event(- AIEvent::ToggleChatWithFile,- toggle_local_ai_chat_file_handler,+ AIEvent::UpdateLocalAISetting,+ update_local_ai_setting_handler,)- .event(- AIEvent::GetModelStorageDirectory,- get_model_storage_directory_handler,- )- .event(AIEvent::GetOfflineAIAppLink, get_offline_app_handler)+ .event(AIEvent::GetAvailableModels, get_model_list_handler).event(AIEvent::CreateChatContext, create_chat_context_handler).event(AIEvent::GetChatInfo, create_chat_context_handler).event(AIEvent::GetChatSettings, get_chat_settings_handler).event(AIEvent::UpdateChatSettings, update_chat_settings_handler).event(AIEvent::RegenerateResponse, regenerate_response_handler)- .event(- AIEvent::GetAvailableModels,- get_available_model_list_handler,- )}#[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)]@@ -88,15 +65,6 @@ pub enum AIEvent {#[event(input = "ChatMessageIdPB", output = "ChatMessagePB")]GetAnswerForQuestion = 5,- #[event(input = "LLMModelPB", output = "LocalModelResourcePB")]- UpdateLocalLLM = 6,-- #[event(output = "LocalModelResourcePB")]- GetLocalLLMState = 7,-- #[event(output = "LLMModelInfoPB")]- RefreshLocalAIModelInfo = 8,-#[event(input = "CompleteTextPB", output = "CompleteTextTaskPB")]CompleteText = 9,@@ -106,26 +74,10 @@ pub enum AIEvent {#[event(input = "ChatFilePB")]ChatWithFile = 11,- #[event(input = "DownloadLLMPB", output = "DownloadTaskPB")]- DownloadLLMResource = 12,-- #[event()]- CancelDownloadLLMResource = 13,-- #[event(output = "LocalAIPluginStatePB")]- GetLocalAIPluginState = 14,-- #[event(output = "LocalAIChatPB")]- ToggleLocalAIChat = 15,-- /// Return Local AI Chat State- #[event(output = "LocalAIChatPB")]- GetLocalAIChatState = 16,-/// Restart local AI chat. When plugin quit or user terminate in task manager or activity monitor,/// the plugin will need to restart.#[event()]- RestartLocalAIChat = 17,+ RestartLocalAI = 17,/// Enable or disable local AI#[event(output = "LocalAIPB")]@@ -135,14 +87,8 @@ pub enum AIEvent {#[event(output = "LocalAIPB")]GetLocalAIState = 19,- #[event()]- ToggleChatWithFile = 20,-- #[event(output = "LocalModelStoragePB")]- GetModelStorageDirectory = 21,-- #[event(output = "OfflineAIPB")]- GetOfflineAIAppLink = 22,+ #[event(output = "LocalAIAppLinkPB")]+ GetLocalAIDownloadLink = 22,#[event(input = "CreateChatContextPB")]CreateChatContext = 23,@@ -161,4 +107,10 @@ pub enum AIEvent {#[event(output = "ModelConfigPB")]GetAvailableModels = 28,++ #[event(output = "LocalAISettingPB")]+ GetLocalAISetting = 29,++ #[event(input = "LocalAISettingPB")]+ UpdateLocalAISetting = 30,}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/local_llm_chat.rs b/frontend/rust-lib/flowy-ai/src/local_ai/controller.rssimilarity index 50%rename from frontend/rust-lib/flowy-ai/src/local_ai/local_llm_chat.rsrename to frontend/rust-lib/flowy-ai/src/local_ai/controller.rsindex ece0bdddda..c44b62be01 100644--- a/frontend/rust-lib/flowy-ai/src/local_ai/local_llm_chat.rs+++ b/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs@@ -1,17 +1,12 @@use crate::ai_manager::AIUserService;-use crate::entities::{LocalAIPluginStatePB, LocalModelResourcePB, RunningStatePB};-use crate::local_ai::local_llm_resource::{LLMResourceService, LocalAIResourceController};+use crate::entities::{LackOfAIResourcePB, LocalAIPB, RunningStatePB};+use crate::local_ai::resource::{LLMResourceService, LocalAIResourceController};use crate::notification::{chat_notification_builder, ChatNotification, APPFLOWY_AI_NOTIFICATION_KEY,};use anyhow::Error;-use appflowy_local_ai::chat_plugin::{AIPluginConfig, AppFlowyLocalAI};use appflowy_plugin::manager::PluginManager;-use appflowy_plugin::util::is_apple_silicon;-use flowy_ai_pub::cloud::{- AppFlowyOfflineAI, ChatCloudService, ChatMessageMetadata, ContextLoader, LLMModel, LocalAIConfig,- SubscriptionPlan,-};+use flowy_ai_pub::cloud::{ChatCloudService, ChatMessageMetadata, ContextLoader, LocalAIConfig};use flowy_error::{FlowyError, FlowyResult};use flowy_sqlite::kv::KVStorePreferences;use futures::Sink;@@ -19,8 +14,10 @@ use lib_infra::async_trait::async_trait;use std::collections::HashMap;use crate::stream_message::StreamMessage;+use appflowy_local_ai::ollama_plugin::OllamaAIPlugin;use arc_swap::ArcSwapOption;use futures_util::SinkExt;+use lib_infra::util::get_operating_system;use serde::{Deserialize, Serialize};use serde_json::json;use std::ops::Deref;@@ -28,38 +25,43 @@ use std::path::{Path, PathBuf};use std::sync::Arc;use tokio::select;use tokio_stream::StreamExt;-use tracing::{debug, error, info, instrument, trace, warn};+use tracing::{debug, error, info, instrument, trace};#[derive(Clone, Debug, Serialize, Deserialize)]-pub struct LLMSetting {- pub app: AppFlowyOfflineAI,- pub llm_model: LLMModel,+pub struct LocalAISetting {+ pub ollama_server_url: String,+ pub chat_model_name: String,+ pub embedding_model_name: String,}-pub struct LLMModelInfo {- pub selected_model: LLMModel,- pub models: Vec<LLMModel>,+impl Default for LocalAISetting {+ fn default() -> Self {+ Self {+ ollama_server_url: "http://localhost:11434".to_string(),+ chat_model_name: "llama3.1".to_string(),+ embedding_model_name: "nomic-embed-text".to_string(),+ }+ }}const APPFLOWY_LOCAL_AI_ENABLED: &str = "appflowy_local_ai_enabled";-const APPFLOWY_LOCAL_AI_CHAT_ENABLED: &str = "appflowy_local_ai_chat_enabled";-const APPFLOWY_LOCAL_AI_CHAT_RAG_ENABLED: &str = "appflowy_local_ai_chat_rag_enabled";-const LOCAL_AI_SETTING_KEY: &str = "appflowy_local_ai_setting:v0";+const LOCAL_AI_SETTING_KEY: &str = "appflowy_local_ai_setting:v1";pub struct LocalAIController {- local_ai: Arc<AppFlowyLocalAI>,- local_ai_resource: Arc<LocalAIResourceController>,+ ai_plugin: Arc<OllamaAIPlugin>,+ resource: Arc<LocalAIResourceController>,current_chat_id: ArcSwapOption<String>,store_preferences: Arc<KVStorePreferences>,user_service: Arc<dyn AIUserService>,+ #[allow(dead_code)]cloud_service: Arc<dyn ChatCloudService>,}impl Deref for LocalAIController {- type Target = Arc<AppFlowyLocalAI>;+ type Target = Arc<OllamaAIPlugin>;fn deref(&self) -> &Self::Target {- &self.local_ai+ &self.ai_plugin}}@@ -70,128 +72,112 @@ impl LocalAIController {user_service: Arc<dyn AIUserService>,cloud_service: Arc<dyn ChatCloudService>,) -> Self {- let local_ai = Arc::new(AppFlowyLocalAI::new(plugin_manager));+ let local_ai = Arc::new(OllamaAIPlugin::new(plugin_manager));let res_impl = LLMResourceServiceImpl {user_service: user_service.clone(),cloud_service: cloud_service.clone(),store_preferences: store_preferences.clone(),};- let (tx, mut rx) = tokio::sync::mpsc::channel(1);- let llm_res = Arc::new(LocalAIResourceController::new(+ let local_ai_resource = Arc::new(LocalAIResourceController::new(user_service.clone(),res_impl,- tx,));let current_chat_id = ArcSwapOption::default();-let mut running_state_rx = local_ai.subscribe_running_state();- let cloned_llm_res = llm_res.clone();+ let cloned_llm_res = local_ai_resource.clone();+ let cloned_store_preferences = store_preferences.clone();tokio::spawn(async move {while let Some(state) = running_state_rx.next().await {info!("[AI Plugin] state: {:?}", state);- let offline_ai_ready = cloned_llm_res.is_offline_app_ready();+ 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::UpdateChatPluginState,+ ChatNotification::UpdateLocalAIState,)- .payload(LocalAIPluginStatePB {+ .payload(LocalAIPB {+ enabled,+ is_app_downloaded: ready,+ lack_of_resource,state: new_state,- offline_ai_ready,}).send();}});- let this = Self {- local_ai,- local_ai_resource: llm_res,+ Self {+ ai_plugin: local_ai,+ resource: local_ai_resource,current_chat_id,store_preferences,user_service,cloud_service,- };+ }+ }- let rag_enabled = this.is_rag_enabled();- let cloned_llm_chat = this.local_ai.clone();- let cloned_llm_res = this.local_ai_resource.clone();- let mut offline_ai_watch = this.local_ai_resource.subscribe_offline_app_state();- tokio::spawn(async move {- let init_fn = || {- if let Ok(chat_config) = cloned_llm_res.get_chat_config(rag_enabled) {- if let Err(err) = initialize_ai_plugin(&cloned_llm_chat, chat_config, None) {- error!("[AI Plugin] failed to setup plugin: {:?}", err);- }- }- };+ #[instrument(level = "debug", skip_all)]+ pub async fn init_plugin_when_first_run(&self) {+ debug!(+ "[AI Plugin] init plugin when first run. thread: {:?}",+ std::thread::current().id()+ );+ let sys = get_operating_system();+ if !sys.is_desktop() {+ return;+ }+ async fn try_init_plugin(+ resource: &Arc<LocalAIResourceController>,+ ai_plugin: &Arc<OllamaAIPlugin>,+ ) {+ if let Err(err) = initialize_ai_plugin(ai_plugin, resource, None).await {+ error!("[AI Plugin] failed to setup plugin: {:?}", err);+ }+ }+ // Clone what is needed for the background task.+ let resource_clone = self.resource.clone();+ let ai_plugin_clone = self.ai_plugin.clone();+ let mut resource_notify = self.resource.subscribe_resource_notify();+ let mut app_state_watcher = self.resource.subscribe_app_state();+ tokio::spawn(async move {loop {select! {- _ = offline_ai_watch.recv() => {- init_fn();- },- _ = rx.recv() => {- init_fn();- },- else => { break; }+ _ = app_state_watcher.recv() => {+ info!("[AI Plugin] app state changed, try to init plugin");+ try_init_plugin(&resource_clone, &ai_plugin_clone).await;+ },+ _ = resource_notify.recv() => {+ info!("[AI Plugin] resource changed, try to init plugin");+ try_init_plugin(&resource_clone, &ai_plugin_clone).await;+ },+ else => break,}}});- if this.can_init_plugin() {- let result = this- .local_ai_resource- .get_chat_config(this.is_rag_enabled());- if let Ok(chat_config) = result {- if let Err(err) = initialize_ai_plugin(&this.local_ai, chat_config, None) {- error!("[AI Plugin] failed to setup plugin: {:?}", err);- }- }+ // Perform initial initialization if required.+ if self.should_init_plugin().await {+ try_init_plugin(&self.resource, &self.ai_plugin).await;+ } else {+ trace!("[AI Plugin] local ai is able to start");}-- this}- pub async fn refresh(&self) -> FlowyResult<()> {- let is_enabled = self.is_enabled();- self.enable_chat_plugin(is_enabled).await?;-- if is_enabled {- let local_ai = self.local_ai.clone();- let workspace_id = self.user_service.workspace_id()?;- let cloned_service = self.cloud_service.clone();- let store_preferences = self.store_preferences.clone();- tokio::spawn(async move {- let key = local_ai_enabled_key(&workspace_id);- match cloned_service.get_workspace_plan(&workspace_id).await {- Ok(plans) => {- trace!("[AI Plugin] workspace:{} plans: {:?}", workspace_id, plans);- if !plans.contains(&SubscriptionPlan::AiLocal) {- info!(- "disable local ai plugin for workspace: {}. reason: no plan found",- workspace_id- );- let _ = store_preferences.set_bool(&key, false);- let _ = local_ai.destroy_chat_plugin().await;- }- },- Err(err) => {- warn!("[AI Plugin]: failed to get workspace plan: {:?}", err);- },- }- });- }+ pub async fn reload(&self) -> FlowyResult<()> {+ let is_enabled = self.is_enabled();+ self.toggle_plugin(is_enabled).await?;Ok(())}- pub async fn refresh_model_info(&self) -> FlowyResult<LLMModelInfo> {- self.local_ai_resource.refresh_llm_resource().await- }-/// Returns true if the local AI is enabled and ready to use.- pub fn can_init_plugin(&self) -> bool {- self.is_enabled() && self.local_ai_resource.is_resource_ready()+ pub async fn should_init_plugin(&self) -> bool {+ self.is_enabled() && self.resource.is_resource_ready().await}/// Indicate whether the local AI plugin is running.@@ -199,7 +185,7 @@ impl LocalAIController {if !self.is_enabled() {return false;}- self.local_ai.get_plugin_running_state().is_ready()+ self.ai_plugin.get_plugin_running_state().is_ready()}/// Indicate whether the local AI is enabled.@@ -217,22 +203,6 @@ impl LocalAIController {}}- /// Indicate whether the local AI chat is enabled. In the future, we can support multiple- /// AI plugin.- pub fn is_chat_enabled(&self) -> bool {- self- .store_preferences- .get_bool(APPFLOWY_LOCAL_AI_CHAT_ENABLED)- .unwrap_or(true)- }-- pub fn is_rag_enabled(&self) -> bool {- self- .store_preferences- .get_bool(APPFLOWY_LOCAL_AI_CHAT_RAG_ENABLED)- .unwrap_or(true)- }-pub fn open_chat(&self, chat_id: &str) {if !self.is_enabled() {return;@@ -249,7 +219,7 @@ impl LocalAIController {.current_chat_id.store(Some(Arc::new(chat_id.to_string())));let chat_id = chat_id.to_string();- let weak_ctrl = Arc::downgrade(&self.local_ai);+ let weak_ctrl = Arc::downgrade(&self.ai_plugin);tokio::spawn(async move {if let Some(ctrl) = weak_ctrl.upgrade() {if let Err(err) = ctrl.create_chat(&chat_id).await {@@ -264,7 +234,7 @@ impl LocalAIController {return;}info!("[AI Plugin] notify close chat: {}", chat_id);- let weak_ctrl = Arc::downgrade(&self.local_ai);+ let weak_ctrl = Arc::downgrade(&self.ai_plugin);let chat_id = chat_id.to_string();tokio::spawn(async move {if let Some(ctrl) = weak_ctrl.upgrade() {@@ -275,91 +245,51 @@ impl LocalAIController {});}- pub async fn select_local_llm(&self, llm_id: i64) -> FlowyResult<LocalModelResourcePB> {- if !self.is_enabled() {- return Err(FlowyError::local_ai_unavailable());- }-- if let Some(model) = self.local_ai_resource.get_selected_model() {- if model.llm_id == llm_id {- return self.local_ai_resource.get_local_llm_state();- }- }-- let state = self.local_ai_resource.use_local_llm(llm_id)?;- // Re-initialize the plugin if the setting is updated and ready to use- if self.local_ai_resource.is_resource_ready() {- let chat_config = self- .local_ai_resource- .get_chat_config(self.is_rag_enabled())?;- if let Err(err) = initialize_ai_plugin(&self.local_ai, chat_config, None) {- error!("failed to setup plugin: {:?}", err);- }- }- Ok(state)- }-- pub async fn get_local_llm_state(&self) -> FlowyResult<LocalModelResourcePB> {- self.local_ai_resource.get_local_llm_state()- }-- pub fn get_current_model(&self) -> Option<LLMModel> {- self.local_ai_resource.get_selected_model()+ pub fn get_local_ai_setting(&self) -> LocalAISetting {+ self.resource.get_llm_setting()}- pub async fn start_downloading<T>(&self, progress_sink: T) -> FlowyResult<String>- where- T: Sink<String, Error = anyhow::Error> + Unpin + Sync + Send + 'static,- {- let task_id = self- .local_ai_resource- .start_downloading(progress_sink)- .await?;- Ok(task_id)- }-- pub fn cancel_download(&self) -> FlowyResult<()> {- self.local_ai_resource.cancel_download()?;+ pub async fn update_local_ai_setting(&self, setting: LocalAISetting) -> FlowyResult<()> {+ info!(+ "[AI Plugin] update local ai setting: {:?}, thread: {:?}",+ setting,+ std::thread::current().id()+ );+ self.destroy_chat_plugin().await?;+ self.resource.set_llm_setting(setting).await?;+ self.reload().await?;Ok(())}- pub fn get_chat_plugin_state(&self) -> LocalAIPluginStatePB {- if !self.is_enabled() {- return LocalAIPluginStatePB {- state: RunningStatePB::Stopped,- offline_ai_ready: false,- };- }-- let offline_ai_ready = self.local_ai_resource.is_offline_app_ready();- let state = self.local_ai.get_plugin_running_state();- LocalAIPluginStatePB {+ pub async fn get_local_ai_state(&self) -> LocalAIPB {+ let enabled = self.is_enabled();+ let is_app_downloaded = self.resource.is_app_downloaded();+ let state = self.ai_plugin.get_plugin_running_state();+ let lack_of_resource = self.resource.get_lack_of_resource().await;+ LocalAIPB {+ enabled,+ is_app_downloaded,state: RunningStatePB::from(state),- offline_ai_ready,+ lack_of_resource,}}- pub fn restart_chat_plugin(&self) {- let rag_enabled = self.is_rag_enabled();- if let Ok(chat_config) = self.local_ai_resource.get_chat_config(rag_enabled) {- if let Err(err) = initialize_ai_plugin(&self.local_ai, chat_config, None) {- error!("[AI Plugin] failed to setup plugin: {:?}", err);- }+ #[instrument(level = "debug", skip_all)]+ pub async fn restart_plugin(&self) {+ if let Err(err) = initialize_ai_plugin(&self.ai_plugin, &self.resource, None).await {+ error!("[AI Plugin] failed to setup plugin: {:?}", err);}}pub fn get_model_storage_directory(&self) -> FlowyResult<String> {self- .local_ai_resource+ .resource.user_model_folder().map(|path| path.to_string_lossy().to_string())}- pub async fn get_offline_ai_app_download_link(&self) -> FlowyResult<String> {- self- .local_ai_resource- .get_offline_ai_app_download_link()- .await+ pub async fn get_plugin_download_link(&self) -> FlowyResult<String> {+ self.resource.get_plugin_download_link().await}pub async fn toggle_local_ai(&self) -> FlowyResult<bool> {@@ -368,45 +298,13 @@ impl LocalAIController {let enabled = !self.store_preferences.get_bool(&key).unwrap_or(true);self.store_preferences.set_bool(&key, enabled)?;- // when enable local ai. we need to check if chat is enabled, if enabled, we need to init chat plugin- // otherwise, we need to destroy the plugin- if enabled {- let chat_enabled = self- .store_preferences- .get_bool(APPFLOWY_LOCAL_AI_CHAT_ENABLED)- .unwrap_or(true);-- if self.local_ai_resource.is_resource_ready() {- self.enable_chat_plugin(chat_enabled).await?;- }- } else {- let _ = self.enable_chat_plugin(false).await;+ if self.resource.is_resource_ready().await {+ self.toggle_plugin(enabled).await?;}- Ok(enabled)- }-- pub async fn toggle_local_ai_chat(&self) -> FlowyResult<bool> {- let enabled = !self- .store_preferences- .get_bool(APPFLOWY_LOCAL_AI_CHAT_ENABLED)- .unwrap_or(true);- self- .store_preferences- .set_bool(APPFLOWY_LOCAL_AI_CHAT_ENABLED, enabled)?;- self.enable_chat_plugin(enabled).await?;Ok(enabled)}- pub async fn toggle_local_ai_chat_rag(&self) -> FlowyResult<bool> {- let enabled = !self- .store_preferences- .get_bool_or_default(APPFLOWY_LOCAL_AI_CHAT_RAG_ENABLED);- self- .store_preferences- .set_bool(APPFLOWY_LOCAL_AI_CHAT_RAG_ENABLED, enabled)?;- Ok(enabled)- }pub async fn index_message_metadata(&self,chat_id: &str,@@ -521,18 +419,20 @@ impl LocalAIController {Ok(())}- async fn enable_chat_plugin(&self, enabled: bool) -> FlowyResult<()> {- info!("[AI Plugin] enable chat plugin: {}", enabled);+ #[instrument(level = "debug", skip_all)]+ async fn toggle_plugin(&self, enabled: bool) -> FlowyResult<()> {+ info!(+ "[AI Plugin] enable: {}, thread id: {:?}",+ enabled,+ std::thread::current().id()+ );if enabled {let (tx, rx) = tokio::sync::oneshot::channel();- let chat_config = self- .local_ai_resource- .get_chat_config(self.is_rag_enabled())?;- if let Err(err) = initialize_ai_plugin(&self.local_ai, chat_config, Some(tx)) {+ if let Err(err) = initialize_ai_plugin(&self.ai_plugin, &self.resource, Some(tx)).await {error!("[AI Plugin] failed to initialize local ai: {:?}", err);}let _ = rx.await;- } else if let Err(err) = self.local_ai.destroy_chat_plugin().await {+ } else if let Err(err) = self.ai_plugin.destroy_chat_plugin().await {error!("[AI Plugin] failed to destroy plugin: {:?}", err);}Ok(())@@ -540,27 +440,70 @@ impl LocalAIController {}#[instrument(level = "debug", skip_all, err)]-fn initialize_ai_plugin(- llm_chat: &Arc<AppFlowyLocalAI>,- mut chat_config: AIPluginConfig,+async fn initialize_ai_plugin(+ llm_chat: &Arc<OllamaAIPlugin>,+ llm_resource: &Arc<LocalAIResourceController>,ret: Option<tokio::sync::oneshot::Sender<()>>,) -> FlowyResult<()> {let llm_chat = llm_chat.clone();+ let lack_of_resource = llm_resource.get_lack_of_resource().await;+ chat_notification_builder(+ APPFLOWY_AI_NOTIFICATION_KEY,+ ChatNotification::UpdateLocalAIState,+ )+ .payload(LocalAIPB {+ enabled: true,+ is_app_downloaded: true,+ state: RunningStatePB::ReadyToRun,+ lack_of_resource: lack_of_resource.clone(),+ })+ .send();++ if let Some(lack_of_resource) = lack_of_resource {+ info!(+ "[AI Plugin] lack of resource: {:?} to initialize plugin, thread: {:?}",+ lack_of_resource,+ std::thread::current().id()+ );+ chat_notification_builder(+ APPFLOWY_AI_NOTIFICATION_KEY,+ ChatNotification::LocalAIResourceUpdated,+ )+ .payload(LackOfAIResourcePB {+ resource_desc: lack_of_resource,+ })+ .send();++ return Ok(());+ }- tokio::spawn(async move {- info!("[AI Plugin] config: {:?}", chat_config);- if is_apple_silicon().await.unwrap_or(false) {- chat_config = chat_config.with_device("gpu");- }- match llm_chat.init_chat_plugin(chat_config).await {- Ok(_) => {},- Err(err) => error!("[AI Plugin] failed to setup plugin: {:?}", err),- }+ let cloned_llm_res = llm_resource.clone();+ tokio::task::spawn_blocking(move || {+ futures::executor::block_on(async move {+ match cloned_llm_res.get_plugin_config(true).await {+ Ok(config) => {+ info!(+ "[AI Plugin] initialize plugin with config: {:?}, thread: {:?}",+ config,+ std::thread::current().id()+ );- if let Some(ret) = ret {- let _ = ret.send(());- }+ match llm_chat.init_chat_plugin(config).await {+ Ok(_) => {},+ Err(err) => error!("[AI Plugin] failed to setup plugin: {:?}", err),+ }++ if let Some(ret) = ret {+ let _ = ret.send(());+ }+ },+ Err(err) => {+ error!("[AI Plugin] failed to get plugin config: {:?}", err);+ },+ };+ })});+Ok(())}@@ -580,17 +523,17 @@ impl LLMResourceService for LLMResourceServiceImpl {Ok(config)}- fn store_setting(&self, setting: LLMSetting) -> Result<(), Error> {+ fn store_setting(&self, setting: LocalAISetting) -> Result<(), Error> {self.store_preferences.set_object(LOCAL_AI_SETTING_KEY, &setting)?;Ok(())}- fn retrieve_setting(&self) -> Option<LLMSetting> {+ fn retrieve_setting(&self) -> Option<LocalAISetting> {self.store_preferences- .get_object::<LLMSetting>(LOCAL_AI_SETTING_KEY)+ .get_object::<LocalAISetting>(LOCAL_AI_SETTING_KEY)}}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/local_llm_resource.rs b/frontend/rust-lib/flowy-ai/src/local_ai/local_llm_resource.rsdeleted file mode 100644index 90dc328a6d..0000000000--- a/frontend/rust-lib/flowy-ai/src/local_ai/local_llm_resource.rs+++ /dev/null@@ -1,534 +0,0 @@-use crate::ai_manager::AIUserService;-use crate::entities::{LocalModelResourcePB, PendingResourcePB, PendingResourceTypePB};-use crate::local_ai::local_llm_chat::{LLMModelInfo, LLMSetting};-use crate::local_ai::model_request::download_model;--use appflowy_local_ai::chat_plugin::AIPluginConfig;-use flowy_ai_pub::cloud::{LLMModel, LocalAIConfig, ModelInfo};-use flowy_error::{ErrorCode, FlowyError, FlowyResult};-use futures::Sink;-use futures_util::SinkExt;-use lib_infra::async_trait::async_trait;--use arc_swap::ArcSwapOption;-use lib_infra::util::{get_operating_system, OperatingSystem};-use std::path::PathBuf;-use std::sync::Arc;--use crate::local_ai::watch::offline_app_path;-#[cfg(target_os = "macos")]-use crate::local_ai::watch::{watch_offline_app, WatchContext};-use tokio::fs::{self};-use tokio_util::sync::CancellationToken;-use tracing::{debug, error, info, instrument, trace, warn};--#[async_trait]-pub trait LLMResourceService: Send + Sync + 'static {- /// Get local ai configuration from remote server- async fn fetch_local_ai_config(&self) -> Result<LocalAIConfig, anyhow::Error>;- fn store_setting(&self, setting: LLMSetting) -> Result<(), anyhow::Error>;- fn retrieve_setting(&self) -> Option<LLMSetting>;-}--const LLM_MODEL_DIR: &str = "models";-const DOWNLOAD_FINISH: &str = "finish";--#[derive(Debug, Clone)]-#[allow(dead_code)]-pub enum WatchDiskEvent {- Create,- Remove,-}--pub enum PendingResource {- OfflineApp,- ModelInfoRes(Vec<ModelInfo>),-}-#[derive(Clone)]-pub struct DownloadTask {- cancel_token: CancellationToken,- tx: tokio::sync::broadcast::Sender<String>,-}-impl DownloadTask {- pub fn new() -> Self {- let (tx, _) = tokio::sync::broadcast::channel(100);- let cancel_token = CancellationToken::new();- Self { cancel_token, tx }- }-- pub fn cancel(&self) {- self.cancel_token.cancel();- }-}--pub struct LocalAIResourceController {- user_service: Arc<dyn AIUserService>,- resource_service: Arc<dyn LLMResourceService>,- llm_setting: ArcSwapOption<LLMSetting>,- // The ai_config will be set when user try to get latest local ai config from server- ai_config: ArcSwapOption<LocalAIConfig>,- download_task: Arc<ArcSwapOption<DownloadTask>>,- resource_notify: tokio::sync::mpsc::Sender<()>,- #[cfg(target_os = "macos")]- #[allow(dead_code)]- offline_app_disk_watch: Option<WatchContext>,- offline_app_state_sender: tokio::sync::broadcast::Sender<WatchDiskEvent>,-}--impl LocalAIResourceController {- pub fn new(- user_service: Arc<dyn AIUserService>,- resource_service: impl LLMResourceService,- resource_notify: tokio::sync::mpsc::Sender<()>,- ) -> Self {- let (offline_app_state_sender, _) = tokio::sync::broadcast::channel(1);- let llm_setting = resource_service.retrieve_setting().map(Arc::new);- #[cfg(target_os = "macos")]- let mut offline_app_disk_watch: Option<WatchContext> = None;-- #[cfg(target_os = "macos")]- {- match watch_offline_app() {- Ok((new_watcher, mut rx)) => {- let sender = offline_app_state_sender.clone();- tokio::spawn(async move {- while let Some(event) = rx.recv().await {- if let Err(err) = sender.send(event) {- error!("[LLM Resource] Failed to send offline app state: {:?}", err);- }- }- });- offline_app_disk_watch = Some(new_watcher);- },- Err(err) => {- error!("[LLM Resource] Failed to watch offline app path: {:?}", err);- },- }- }-- Self {- user_service,- resource_service: Arc::new(resource_service),- llm_setting: ArcSwapOption::new(llm_setting),- ai_config: Default::default(),- download_task: Default::default(),- resource_notify,- #[cfg(target_os = "macos")]- offline_app_disk_watch,- offline_app_state_sender,- }- }-- #[allow(dead_code)]- pub fn subscribe_offline_app_state(&self) -> tokio::sync::broadcast::Receiver<WatchDiskEvent> {- self.offline_app_state_sender.subscribe()- }-- fn set_llm_setting(&self, llm_setting: LLMSetting) {- self.llm_setting.store(Some(llm_setting.into()));- }-- /// Returns true when all resources are downloaded and ready to use.- pub fn is_resource_ready(&self) -> bool {- match self.calculate_pending_resources() {- Ok(res) => res.is_empty(),- Err(_) => false,- }- }-- pub fn is_offline_app_ready(&self) -> bool {- offline_app_path().exists()- }-- pub async fn get_offline_ai_app_download_link(&self) -> FlowyResult<String> {- let ai_config = self.fetch_ai_config().await?;- Ok(ai_config.plugin.url)- }-- /// Retrieves model information and updates the current model settings.- #[instrument(level = "debug", skip_all, err)]- pub async fn refresh_llm_resource(&self) -> FlowyResult<LLMModelInfo> {- let ai_config = self.fetch_ai_config().await?;- if ai_config.models.is_empty() {- return Err(FlowyError::local_ai().with_context("No model found"));- }-- self.ai_config.store(Some(ai_config.clone().into()));- let selected_model = self.select_model(&ai_config)?;-- let llm_setting = LLMSetting {- app: ai_config.plugin.clone(),- llm_model: selected_model.clone(),- };- self.set_llm_setting(llm_setting.clone());- self.resource_service.store_setting(llm_setting)?;-- Ok(LLMModelInfo {- selected_model,- models: ai_config.models,- })- }-- #[instrument(level = "info", skip_all, err)]- pub fn use_local_llm(&self, llm_id: i64) -> FlowyResult<LocalModelResourcePB> {- let (app, llm_model) = self- .ai_config- .load()- .as_ref()- .and_then(|config| {- config- .models- .iter()- .find(|model| model.llm_id == llm_id)- .cloned()- .map(|model| (config.plugin.clone(), model))- })- .ok_or_else(|| FlowyError::local_ai().with_context("No local ai config found"))?;-- let llm_setting = LLMSetting {- app,- llm_model: llm_model.clone(),- };-- trace!("[LLM Resource] Selected AI setting: {:?}", llm_setting);- self.set_llm_setting(llm_setting.clone());- self.resource_service.store_setting(llm_setting)?;- self.get_local_llm_state()- }-- pub fn get_local_llm_state(&self) -> FlowyResult<LocalModelResourcePB> {- let state = self- .check_resource()- .ok_or_else(|| FlowyError::local_ai().with_context("No local ai config found"))?;- Ok(state)- }-- #[instrument(level = "debug", skip_all)]- fn check_resource(&self) -> Option<LocalModelResourcePB> {- trace!("[LLM Resource] Checking local ai resources");-- let pending_resources = self.calculate_pending_resources().ok()?;- let is_ready = pending_resources.is_empty();- let is_downloading = self.download_task.load().is_some();- let pending_resources: Vec<_> = pending_resources- .into_iter()- .flat_map(|res| match res {- PendingResource::OfflineApp => vec![PendingResourcePB {- name: "AppFlowy Plugin".to_string(),- file_size: "0 GB".to_string(),- requirements: "".to_string(),- res_type: PendingResourceTypePB::OfflineApp,- }],- PendingResource::ModelInfoRes(model_infos) => model_infos- .into_iter()- .map(|model_info| PendingResourcePB {- name: model_info.name,- file_size: bytes_to_readable_format(model_info.file_size as u64),- requirements: model_info.requirements,- res_type: PendingResourceTypePB::AIModel,- })- .collect::<Vec<_>>(),- })- .collect();-- let resource = LocalModelResourcePB {- is_ready,- pending_resources,- is_downloading,- };-- debug!("[LLM Resource] Local AI resources state: {:?}", resource);- Some(resource)- }-- /// Returns true when all resources are downloaded and ready to use.- pub fn calculate_pending_resources(&self) -> FlowyResult<Vec<PendingResource>> {- match self.llm_setting.load().as_ref() {- None => Err(FlowyError::local_ai().with_context("Can't find any llm config")),- Some(llm_setting) => {- let mut resources = vec![];- let app_path = offline_app_path();- if !app_path.exists() {- trace!("[LLM Resource] offline app not found: {:?}", app_path);- resources.push(PendingResource::OfflineApp);- }-- let chat_model = self.model_path(&llm_setting.llm_model.chat_model.file_name)?;- if !chat_model.exists() {- resources.push(PendingResource::ModelInfoRes(vec![llm_setting- .llm_model- .chat_model- .clone()]));- }-- let embedding_model = self.model_path(&llm_setting.llm_model.embedding_model.file_name)?;- if !embedding_model.exists() {- resources.push(PendingResource::ModelInfoRes(vec![llm_setting- .llm_model- .embedding_model- .clone()]));- }-- Ok(resources)- },- }- }-- #[instrument(level = "info", skip_all, err)]- pub async fn start_downloading<T>(&self, mut progress_sink: T) -> FlowyResult<String>- where- T: Sink<String, Error = anyhow::Error> + Unpin + Sync + Send + 'static,- {- let task_id = uuid::Uuid::new_v4().to_string();- let weak_download_task = Arc::downgrade(&self.download_task);- let resource_notify = self.resource_notify.clone();- // notify download progress to client.- let progress_notify = |mut rx: tokio::sync::broadcast::Receiver<String>| {- tokio::spawn(async move {- while let Ok(value) = rx.recv().await {- let is_finish = value == DOWNLOAD_FINISH;- if let Err(err) = progress_sink.send(value).await {- warn!("Failed to send progress: {:?}", err);- break;- }-- if is_finish {- info!("notify download finish, need to reload resources");- let _ = resource_notify.send(()).await;- if let Some(download_task) = weak_download_task.upgrade() {- if let Some(task) = download_task.swap(None) {- task.cancel();- }- }- break;- }- }- });- };-- // return immediately if download task already exists- {- let guard = self.download_task.load();- if let Some(download_task) = &*guard {- trace!(- "Download task already exists, return the task id: {}",- task_id- );- progress_notify(download_task.tx.subscribe());- return Ok(task_id);- }- }-- // If download task is not exists, create a new download task.- info!("[LLM Resource] Start new download task");- let llm_setting = self- .llm_setting- .load_full()- .ok_or_else(|| FlowyError::local_ai().with_context("No local ai config found"))?;-- let download_task = Arc::new(DownloadTask::new());- self.download_task.store(Some(download_task.clone()));- progress_notify(download_task.tx.subscribe());-- let model_dir = self.user_model_folder()?;- if !model_dir.exists() {- fs::create_dir_all(&model_dir).await.map_err(|err| {- FlowyError::local_ai().with_context(format!("Failed to create model dir: {:?}", err))- })?;- }-- tokio::spawn(async move {- // After download the plugin, start downloading models- let chat_model_file = (- model_dir.join(&llm_setting.llm_model.chat_model.file_name),- &llm_setting.llm_model.chat_model.file_name,- &llm_setting.llm_model.chat_model.name,- &llm_setting.llm_model.chat_model.download_url,- );- let embedding_model_file = (- model_dir.join(&llm_setting.llm_model.embedding_model.file_name),- &llm_setting.llm_model.embedding_model.file_name,- &llm_setting.llm_model.embedding_model.name,- &llm_setting.llm_model.embedding_model.download_url,- );- for (file_path, file_name, model_name, url) in [chat_model_file, embedding_model_file] {- if file_path.exists() {- continue;- }-- info!("[LLM Resource] Downloading model: {:?}", file_name);- let plugin_progress_tx = download_task.tx.clone();- let cloned_model_name = model_name.clone();- let progress = Arc::new(move |downloaded, total_size| {- let progress = (downloaded as f64 / total_size as f64).clamp(0.0, 1.0);- if plugin_progress_tx.receiver_count() == 0 {- return;- }-- if let Err(err) =- plugin_progress_tx.send(format!("{}:progress:{}", cloned_model_name, progress))- {- warn!("Failed to send progress: {:?}", err);- }- });- match download_model(- url,- &model_dir,- file_name,- Some(progress),- Some(download_task.cancel_token.clone()),- )- .await- {- Ok(_) => info!("[LLM Resource] Downloaded model: {:?}", file_name),- Err(err) => {- error!(- "[LLM Resource] Failed to download model for given url: {:?}, error: {:?}",- url, err- );- download_task- .tx- .send(format!("error:failed to download {}", model_name))?;- continue;- },- }- }- info!("[LLM Resource] All resources downloaded");- download_task.tx.send(DOWNLOAD_FINISH.to_string())?;- Ok::<_, anyhow::Error>(())- });-- Ok(task_id)- }-- pub fn cancel_download(&self) -> FlowyResult<()> {- if let Some(cancel_token) = self.download_task.swap(None) {- info!("[LLM Resource] Cancel download");- cancel_token.cancel();- }-- Ok(())- }-- #[instrument(level = "info", skip_all)]- pub fn get_chat_config(&self, rag_enabled: bool) -> FlowyResult<AIPluginConfig> {- if !self.is_resource_ready() {- return Err(FlowyError::local_ai().with_context("Local AI resources are not ready"));- }-- let llm_setting = self- .llm_setting- .load_full()- .ok_or_else(|| FlowyError::local_ai().with_context("No local llm setting found"))?;-- let model_dir = self.user_model_folder()?;- let bin_path = match get_operating_system() {- OperatingSystem::MacOS => {- let path = offline_app_path();- if !path.exists() {- return Err(FlowyError::new(- ErrorCode::AIOfflineNotInstalled,- format!("AppFlowy Offline not installed at path: {:?}", path),- ));- }- path- },- _ => {- return Err(- FlowyError::local_ai_unavailable()- .with_context("Local AI not available on current platform"),- );- },- };-- let chat_model_path = model_dir.join(&llm_setting.llm_model.chat_model.file_name);- let mut config = AIPluginConfig::new(bin_path, chat_model_path)?;-- if rag_enabled {- let resource_dir = self.resource_dir()?;- let embedding_model_path = model_dir.join(&llm_setting.llm_model.embedding_model.file_name);- let persist_directory = resource_dir.join("vectorstore");- if !persist_directory.exists() {- std::fs::create_dir_all(&persist_directory)?;- }- config.set_rag_enabled(&embedding_model_path, &persist_directory)?;- }-- if cfg!(debug_assertions) {- config = config.with_verbose(true);- }- trace!("[AI Chat] use config: {:?}", config);- Ok(config)- }-- /// Fetches the local AI configuration from the resource service.- async fn fetch_ai_config(&self) -> FlowyResult<LocalAIConfig> {- self- .resource_service- .fetch_local_ai_config()- .await- .map_err(|err| {- error!("[LLM Resource] Failed to fetch local ai config: {:?}", err);- FlowyError::local_ai()- .with_context("Can't retrieve model info. Please try again later".to_string())- })- }-- pub fn get_selected_model(&self) -> Option<LLMModel> {- let setting = self.llm_setting.load();- Some(setting.as_ref()?.llm_model.clone())- }-- /// Selects the appropriate model based on the current settings or defaults to the first model.- fn select_model(&self, ai_config: &LocalAIConfig) -> FlowyResult<LLMModel> {- let llm_setting = self.llm_setting.load();- let selected_model = match &*llm_setting {- None => ai_config.models[0].clone(),- Some(llm_setting) => {- match ai_config- .models- .iter()- .find(|model| model.llm_id == llm_setting.llm_model.llm_id)- {- None => ai_config.models[0].clone(),- Some(llm_model) => {- if llm_model != &llm_setting.llm_model {- info!(- "[LLM Resource] existing model is different from remote, replace with remote model"- );- }- llm_model.clone()- },- }- },- };- Ok(selected_model)- }-- pub(crate) fn user_model_folder(&self) -> FlowyResult<PathBuf> {- self.resource_dir().map(|dir| dir.join(LLM_MODEL_DIR))- }-- fn model_path(&self, model_file_name: &str) -> FlowyResult<PathBuf> {- self- .user_model_folder()- .map(|dir| dir.join(model_file_name))- }-- pub(crate) fn resource_dir(&self) -> FlowyResult<PathBuf> {- let user_data_dir = self.user_service.application_root_dir()?;- Ok(user_data_dir.join("ai"))- }-}-fn bytes_to_readable_format(bytes: u64) -> String {- const BYTES_IN_GIGABYTE: u64 = 1024 * 1024 * 1024;- const BYTES_IN_MEGABYTE: u64 = 1024 * 1024;-- if bytes >= BYTES_IN_GIGABYTE {- let gigabytes = (bytes as f64) / (BYTES_IN_GIGABYTE as f64);- format!("{:.1} GB", gigabytes)- } else {- let megabytes = (bytes as f64) / (BYTES_IN_MEGABYTE as f64);- format!("{:.2} MB", megabytes)- }-}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/mod.rs b/frontend/rust-lib/flowy-ai/src/local_ai/mod.rsindex 5daddf881b..c0fd967d43 100644--- a/frontend/rust-lib/flowy-ai/src/local_ai/mod.rs+++ b/frontend/rust-lib/flowy-ai/src/local_ai/mod.rs@@ -1,6 +1,6 @@-pub mod local_llm_chat;-pub mod local_llm_resource;-mod model_request;+pub mod controller;+mod request;+pub mod resource;pub mod stream_util;pub mod watch;diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/model_request.rs b/frontend/rust-lib/flowy-ai/src/local_ai/request.rssimilarity index 99%rename from frontend/rust-lib/flowy-ai/src/local_ai/model_request.rsrename to frontend/rust-lib/flowy-ai/src/local_ai/request.rsindex c37a6f04ff..dd94b43041 100644--- a/frontend/rust-lib/flowy-ai/src/local_ai/model_request.rs+++ b/frontend/rust-lib/flowy-ai/src/local_ai/request.rs@@ -12,6 +12,7 @@ use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};use tokio_util::sync::CancellationToken;use tracing::{instrument, trace};+#[allow(dead_code)]type ProgressCallback = Arc<dyn Fn(u64, u64) + Send + Sync>;#[instrument(level = "trace", skip_all, err)]@@ -95,6 +96,7 @@ pub async fn download_model(Ok(download_path)}+#[allow(dead_code)]async fn make_request(client: &Client,url: &str,diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/resource.rs b/frontend/rust-lib/flowy-ai/src/local_ai/resource.rsnew file mode 100644index 0000000000..15bde0e431--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/resource.rs@@ -0,0 +1,315 @@+use crate::ai_manager::AIUserService;+use crate::local_ai::controller::LocalAISetting;+use flowy_ai_pub::cloud::LocalAIConfig;+use flowy_error::{ErrorCode, FlowyError, FlowyResult};+use lib_infra::async_trait::async_trait;++use crate::entities::LackOfAIResourcePB;+use crate::local_ai::watch::ollama_plugin_path;+#[cfg(target_os = "macos")]+use crate::local_ai::watch::{watch_offline_app, WatchContext};+use crate::notification::{+ chat_notification_builder, ChatNotification, APPFLOWY_AI_NOTIFICATION_KEY,+};+use appflowy_local_ai::ollama_plugin::OllamaPluginConfig;+use lib_infra::util::{get_operating_system, OperatingSystem};+use reqwest::Client;+use std::path::PathBuf;+use std::sync::Arc;+use std::time::Duration;+use tracing::{error, info, instrument, trace};++#[async_trait]+pub trait LLMResourceService: Send + Sync + 'static {+ /// Get local ai configuration from remote server+ async fn fetch_local_ai_config(&self) -> Result<LocalAIConfig, anyhow::Error>;+ fn store_setting(&self, setting: LocalAISetting) -> Result<(), anyhow::Error>;+ fn retrieve_setting(&self) -> Option<LocalAISetting>;+}++const LLM_MODEL_DIR: &str = "models";++#[derive(Debug, Clone)]+#[allow(dead_code)]+pub enum WatchDiskEvent {+ Create,+ Remove,+}++pub enum PendingResource {+ LocalAIAppNotDownloaded,+ OllamaServerNotFound,+ OllamaNotInstalled,+ MissingModel(String),+}++impl PendingResource {+ pub fn desc(self) -> String {+ match self {+ PendingResource::LocalAIAppNotDownloaded => "Local AI app not downloaded".to_string(),+ PendingResource::OllamaServerNotFound => "Ollama server not ready".to_string(),+ PendingResource::OllamaNotInstalled => "Ollama not installed".to_string(),+ PendingResource::MissingModel(model) => format!("Missing model: {}", model),+ }+ }+}++pub struct LocalAIResourceController {+ user_service: Arc<dyn AIUserService>,+ resource_service: Arc<dyn LLMResourceService>,+ resource_notify: tokio::sync::broadcast::Sender<()>,+ #[cfg(target_os = "macos")]+ #[allow(dead_code)]+ app_disk_watch: Option<WatchContext>,+ app_state_sender: tokio::sync::broadcast::Sender<WatchDiskEvent>,+}++impl LocalAIResourceController {+ pub fn new(+ user_service: Arc<dyn AIUserService>,+ resource_service: impl LLMResourceService,+ ) -> Self {+ let (resource_notify, _) = tokio::sync::broadcast::channel(1);+ let (app_state_sender, _) = tokio::sync::broadcast::channel(1);+ #[cfg(target_os = "macos")]+ let mut offline_app_disk_watch: Option<WatchContext> = None;++ #[cfg(target_os = "macos")]+ {+ match watch_offline_app() {+ Ok((new_watcher, mut rx)) => {+ let sender = app_state_sender.clone();+ tokio::spawn(async move {+ while let Some(event) = rx.recv().await {+ if let Err(err) = sender.send(event) {+ error!("[LLM Resource] Failed to send offline app state: {:?}", err);+ }+ }+ });+ offline_app_disk_watch = Some(new_watcher);+ },+ Err(err) => {+ error!("[LLM Resource] Failed to watch offline app path: {:?}", err);+ },+ }+ }++ Self {+ user_service,+ resource_service: Arc::new(resource_service),+ #[cfg(target_os = "macos")]+ app_disk_watch: offline_app_disk_watch,+ app_state_sender,+ resource_notify,+ }+ }++ pub fn subscribe_resource_notify(&self) -> tokio::sync::broadcast::Receiver<()> {+ self.resource_notify.subscribe()+ }++ pub fn subscribe_app_state(&self) -> tokio::sync::broadcast::Receiver<WatchDiskEvent> {+ self.app_state_sender.subscribe()+ }++ /// Returns true when all resources are downloaded and ready to use.+ pub async fn is_resource_ready(&self) -> bool {+ let sys = get_operating_system();+ if !sys.is_desktop() {+ return false;+ }++ match self.calculate_pending_resources().await {+ Ok(res) => res.is_empty(),+ Err(_) => false,+ }+ }++ pub fn is_app_downloaded(&self) -> bool {+ ollama_plugin_path().exists()+ }++ pub async fn get_plugin_download_link(&self) -> FlowyResult<String> {+ let ai_config = self.get_local_ai_configuration().await?;+ Ok(ai_config.plugin.url)+ }++ /// Retrieves model information and updates the current model settings.+ pub fn get_llm_setting(&self) -> LocalAISetting {+ self.resource_service.retrieve_setting().unwrap_or_default()+ }++ #[instrument(level = "info", skip_all, err)]+ pub async fn set_llm_setting(&self, setting: LocalAISetting) -> FlowyResult<()> {+ self.resource_service.store_setting(setting)?;+ let mut resources = self.calculate_pending_resources().await?;+ if let Some(resource) = resources.pop() {+ chat_notification_builder(+ APPFLOWY_AI_NOTIFICATION_KEY,+ ChatNotification::LocalAIResourceUpdated,+ )+ .payload(LackOfAIResourcePB {+ resource_desc: resource.desc(),+ })+ .send();+ }+ Ok(())+ }++ pub async fn get_lack_of_resource(&self) -> Option<String> {+ let mut resources = self.calculate_pending_resources().await.ok()?;+ resources.pop().map(|r| r.desc())+ }++ /// Returns true when all resources are downloaded and ready to use.+ pub async fn calculate_pending_resources(&self) -> FlowyResult<Vec<PendingResource>> {+ let mut resources = vec![];+ let app_path = ollama_plugin_path();+ if !app_path.exists() {+ trace!("[LLM Resource] offline app not found: {:?}", app_path);+ resources.push(PendingResource::LocalAIAppNotDownloaded);+ }++ let setting = self.get_llm_setting();+ let client = Client::builder().timeout(Duration::from_secs(5)).build()?;+ match client.get(&setting.ollama_server_url).send().await {+ Ok(resp) if resp.status().is_success() => {+ info!(+ "[LLM Resource] Ollama server is running at {}",+ setting.ollama_server_url+ );+ },+ _ => {+ info!(+ "[LLM Resource] Ollama server is not responding at {}",+ setting.ollama_server_url+ );+ resources.push(PendingResource::OllamaServerNotFound);+ return Ok(resources);+ },+ }++ let required_models = vec![+ setting.chat_model_name,+ setting.embedding_model_name,+ // Add any additional required models here.+ ];+ match tokio::process::Command::new("ollama")+ .arg("list")+ .output()+ .await+ {+ Ok(output) if output.status.success() => {+ let stdout = String::from_utf8_lossy(&output.stdout);+ for model in &required_models {+ if !stdout.contains(model.as_str()) {+ log::trace!(+ "[LLM Resource] required model '{}' not found in ollama list",+ model+ );+ resources.push(PendingResource::MissingModel(model.clone()));+ return Ok(resources);+ }+ }+ },+ Ok(output) => {+ error!(+ "[LLM Resource] 'ollama list' command failed with status: {:?}",+ output.status+ );+ resources.push(PendingResource::OllamaNotInstalled);+ },+ Err(e) => {+ error!("[LLM Resource] failed to execute 'ollama list': {:?}", e);+ resources.push(PendingResource::OllamaNotInstalled);+ },+ }++ Ok(resources)+ }++ #[instrument(level = "info", skip_all)]+ pub async fn get_plugin_config(&self, rag_enabled: bool) -> FlowyResult<OllamaPluginConfig> {+ if !self.is_resource_ready().await {+ return Err(FlowyError::local_ai().with_context("Local AI resources are not ready"));+ }++ let llm_setting = self.get_llm_setting();+ let bin_path = match get_operating_system() {+ OperatingSystem::MacOS => {+ let path = ollama_plugin_path();+ if !path.exists() {+ return Err(FlowyError::new(+ ErrorCode::AIOfflineNotInstalled,+ format!("AppFlowy Offline not installed at path: {:?}", path),+ ));+ }+ path+ },+ _ => {+ return Err(+ FlowyError::local_ai_unavailable()+ .with_context("Local AI not available on current platform"),+ );+ },+ };++ let mut config = OllamaPluginConfig::new(+ bin_path,+ llm_setting.chat_model_name.clone(),+ llm_setting.embedding_model_name.clone(),+ Some(llm_setting.ollama_server_url.clone()),+ )?;++ if rag_enabled {+ let resource_dir = self.resource_dir()?;+ let persist_directory = resource_dir.join("vectorstore");+ if !persist_directory.exists() {+ std::fs::create_dir_all(&persist_directory)?;+ }+ config.set_rag_enabled(&persist_directory)?;+ }++ if cfg!(debug_assertions) {+ config = config.with_verbose(true);+ }+ trace!("[AI Chat] config: {:?}", config);+ Ok(config)+ }++ /// Fetches the local AI configuration from the resource service.+ async fn get_local_ai_configuration(&self) -> FlowyResult<LocalAIConfig> {+ self+ .resource_service+ .fetch_local_ai_config()+ .await+ .map_err(|err| {+ error!("[LLM Resource] Failed to fetch local ai config: {:?}", err);+ FlowyError::local_ai()+ .with_context("Can't retrieve model info. Please try again later".to_string())+ })+ }++ pub(crate) fn user_model_folder(&self) -> FlowyResult<PathBuf> {+ self.resource_dir().map(|dir| dir.join(LLM_MODEL_DIR))+ }++ pub(crate) fn resource_dir(&self) -> FlowyResult<PathBuf> {+ let user_data_dir = self.user_service.application_root_dir()?;+ Ok(user_data_dir.join("ai"))+ }+}++#[allow(dead_code)]+fn bytes_to_readable_format(bytes: u64) -> String {+ const BYTES_IN_GIGABYTE: u64 = 1024 * 1024 * 1024;+ const BYTES_IN_MEGABYTE: u64 = 1024 * 1024;++ if bytes >= BYTES_IN_GIGABYTE {+ let gigabytes = (bytes as f64) / (BYTES_IN_GIGABYTE as f64);+ format!("{:.1} GB", gigabytes)+ } else {+ let megabytes = (bytes as f64) / (BYTES_IN_MEGABYTE as f64);+ format!("{:.2} MB", megabytes)+ }+}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/watch.rs b/frontend/rust-lib/flowy-ai/src/local_ai/watch.rsindex cee8f1d381..4c88354ba7 100644--- a/frontend/rust-lib/flowy-ai/src/local_ai/watch.rs+++ b/frontend/rust-lib/flowy-ai/src/local_ai/watch.rs@@ -1,4 +1,4 @@-use crate::local_ai::local_llm_resource::WatchDiskEvent;+use crate::local_ai::resource::WatchDiskEvent;use flowy_error::{FlowyError, FlowyResult};use std::path::PathBuf;use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};@@ -20,7 +20,7 @@ pub fn watch_offline_app() -> FlowyResult<(WatchContext, UnboundedReceiver<WatchFlowyError::internal().with_context("Unsupported platform for offline app watching")})?;let (tx, rx) = unbounded_channel();- let app_path = offline_app_path();+ let app_path = ollama_plugin_path();let mut watcher = notify::recommended_watcher(move |res: Result<Event, _>| match res {Ok(event) => {if event.paths.iter().any(|path| path == &app_path) {@@ -81,8 +81,8 @@ pub(crate) fn offline_app_path() -> PathBuf {}#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]-pub(crate) fn offline_app_path() -> PathBuf {- let offline_app = "appflowy_ai_plugin";+pub(crate) fn ollama_plugin_path() -> PathBuf {+ let offline_app = "ollama_ai_plugin";#[cfg(target_os = "windows")]return PathBuf::from(format!("/usr/local/bin/{}", offline_app));diff --git a/frontend/rust-lib/flowy-ai/src/middleware/chat_service_mw.rs b/frontend/rust-lib/flowy-ai/src/middleware/chat_service_mw.rsindex d6dce3696c..6fb41c84f2 100644--- a/frontend/rust-lib/flowy-ai/src/middleware/chat_service_mw.rs+++ b/frontend/rust-lib/flowy-ai/src/middleware/chat_service_mw.rs@@ -1,6 +1,6 @@use crate::ai_manager::AIUserService;use crate::entities::{ChatStatePB, ModelTypePB};-use crate::local_ai::local_llm_chat::LocalAIController;+use crate::local_ai::controller::LocalAIController;use crate::notification::{chat_notification_builder, ChatNotification, APPFLOWY_AI_NOTIFICATION_KEY,};@@ -30,7 +30,7 @@ use tracing::trace;pub struct AICloudServiceMiddleware {cloud_service: Arc<dyn ChatCloudService>,user_service: Arc<dyn AIUserService>,- local_llm_controller: Arc<LocalAIController>,+ local_ai: Arc<LocalAIController>,storage_service: Weak<dyn StorageService>,}@@ -38,19 +38,19 @@ impl AICloudServiceMiddleware {pub fn new(user_service: Arc<dyn AIUserService>,cloud_service: Arc<dyn ChatCloudService>,- local_llm_controller: Arc<LocalAIController>,+ local_ai: Arc<LocalAIController>,storage_service: Weak<dyn StorageService>,) -> Self {Self {user_service,cloud_service,- local_llm_controller,+ local_ai,storage_service,}}pub fn is_local_ai_enabled(&self) -> bool {- self.local_llm_controller.is_enabled()+ self.local_ai.is_enabled()}pub async fn index_message_metadata(@@ -68,7 +68,7 @@ impl AICloudServiceMiddleware {.await;self- .local_llm_controller+ .local_ai.index_message_metadata(chat_id, metadata_list, index_process_sink).await?;let _ = index_process_sink@@ -97,7 +97,7 @@ impl AICloudServiceMiddleware {) {chat_notification_builder(APPFLOWY_AI_NOTIFICATION_KEY,- ChatNotification::UpdateChatPluginState,+ ChatNotification::UpdateLocalAIState,).payload(ChatStatePB {model_type: ModelTypePB::LocalAI,@@ -158,11 +158,11 @@ impl ChatCloudService for AICloudServiceMiddleware {question_id: i64,format: ResponseFormat,) -> Result<StreamAnswer, FlowyError> {- if self.local_llm_controller.is_running() {+ if self.local_ai.is_running() {let row = self.get_message_record(question_id)?;match self- .local_llm_controller- .stream_question(chat_id, &row.content, json!([]))+ .local_ai+ .stream_question(chat_id, &row.content, json!({})).await{Ok(stream) => Ok(QuestionStream::new(stream).boxed()),@@ -185,13 +185,9 @@ impl ChatCloudService for AICloudServiceMiddleware {chat_id: &str,question_message_id: i64,) -> Result<ChatMessage, FlowyError> {- if self.local_llm_controller.is_running() {+ if self.local_ai.is_running() {let content = self.get_message_record(question_message_id)?.content;- match self- .local_llm_controller- .ask_question(chat_id, &content)- .await- {+ match self.local_ai.ask_question(chat_id, &content).await {Ok(answer) => {// TODO(nathan): metadatalet message = self@@ -244,9 +240,9 @@ impl ChatCloudService for AICloudServiceMiddleware {chat_id: &str,message_id: i64,) -> Result<RepeatedRelatedQuestion, FlowyError> {- if self.local_llm_controller.is_running() {+ if self.local_ai.is_running() {let questions = self- .local_llm_controller+ .local_ai.get_related_question(chat_id).await.map_err(|err| FlowyError::local_ai().with_context(err))?;@@ -274,9 +270,9 @@ impl ChatCloudService for AICloudServiceMiddleware {workspace_id: &str,params: CompleteTextParams,) -> Result<StreamComplete, FlowyError> {- if self.local_llm_controller.is_running() {+ if self.local_ai.is_running() {match self- .local_llm_controller+ .local_ai.complete_text(¶ms.text, params.completion_type.unwrap() as u8).await{@@ -305,9 +301,9 @@ impl ChatCloudService for AICloudServiceMiddleware {chat_id: &str,metadata: Option<HashMap<String, Value>>,) -> Result<(), FlowyError> {- if self.local_llm_controller.is_running() {+ if self.local_ai.is_running() {self- .local_llm_controller+ .local_ai.index_file(chat_id, Some(file_path.to_path_buf()), None, metadata).await.map_err(|err| FlowyError::local_ai().with_context(err))?;diff --git a/frontend/rust-lib/flowy-ai/src/notification.rs b/frontend/rust-lib/flowy-ai/src/notification.rsindex c7857dbc8a..81d1bb92d6 100644--- a/frontend/rust-lib/flowy-ai/src/notification.rs+++ b/frontend/rust-lib/flowy-ai/src/notification.rs@@ -12,9 +12,9 @@ pub enum ChatNotification {DidReceiveChatMessage = 3,StreamChatMessageError = 4,FinishStreaming = 5,- UpdateChatPluginState = 6,- UpdateLocalChatAI = 7,- DidUpdateChatSettings = 8,+ UpdateLocalAIState = 6,+ DidUpdateChatSettings = 7,+ LocalAIResourceUpdated = 8,}impl std::convert::From<ChatNotification> for i32 {@@ -30,8 +30,9 @@ impl std::convert::From<i32> for ChatNotification {3 => ChatNotification::DidReceiveChatMessage,4 => ChatNotification::StreamChatMessageError,5 => ChatNotification::FinishStreaming,- 6 => ChatNotification::UpdateChatPluginState,- 7 => ChatNotification::UpdateLocalChatAI,+ 6 => ChatNotification::UpdateLocalAIState,+ 7 => ChatNotification::DidUpdateChatSettings,+ 8 => ChatNotification::LocalAIResourceUpdated,_ => ChatNotification::Unknown,}}diff --git a/frontend/rust-lib/flowy-core/src/deps_resolve/database_deps.rs b/frontend/rust-lib/flowy-core/src/deps_resolve/database_deps.rsindex 56ac310300..328b90aa15 100644--- a/frontend/rust-lib/flowy-core/src/deps_resolve/database_deps.rs+++ b/frontend/rust-lib/flowy-core/src/deps_resolve/database_deps.rs@@ -51,10 +51,10 @@ impl DatabaseAIService for DatabaseAIServiceMiddleware {object_id: &str,summary_row: SummaryRowContent,) -> Result<String, FlowyError> {- if self.ai_manager.local_ai_controller.is_running() {+ if self.ai_manager.local_ai.is_running() {self.ai_manager- .local_ai_controller+ .local_ai.summary_database_row(summary_row).await.map_err(|err| FlowyError::local_ai().with_context(err))@@ -72,7 +72,7 @@ impl DatabaseAIService for DatabaseAIServiceMiddleware {translate_row: TranslateRowContent,language: &str,) -> Result<TranslateRowResponse, FlowyError> {- if self.ai_manager.local_ai_controller.is_running() {+ if self.ai_manager.local_ai.is_running() {let data = LocalAITranslateRowData {cells: translate_row.into_iter()@@ -86,7 +86,7 @@ impl DatabaseAIService for DatabaseAIServiceMiddleware {};let resp = self.ai_manager- .local_ai_controller+ .local_ai.translate_database_row(data).await.map_err(|err| FlowyError::local_ai().with_context(err))?;diff --git a/frontend/rust-lib/flowy-folder/Cargo.toml b/frontend/rust-lib/flowy-folder/Cargo.tomlindex 30d9850e55..1a36ecce2c 100644--- a/frontend/rust-lib/flowy-folder/Cargo.toml+++ b/frontend/rust-lib/flowy-folder/Cargo.toml@@ -41,7 +41,7 @@ validator.workspace = trueasync-trait.workspace = trueclient-api = { workspace = true }regex = "1.9.5"-futures = "0.3.30"+futures = "0.3.31"dashmap.workspace = truediff --git a/frontend/rust-lib/lib-infra/Cargo.toml b/frontend/rust-lib/lib-infra/Cargo.tomlindex 12c805862b..a07a3413f4 100644--- a/frontend/rust-lib/lib-infra/Cargo.toml+++ b/frontend/rust-lib/lib-infra/Cargo.toml@@ -22,7 +22,7 @@ validator = { workspace = true, features = ["derive"] }tracing.workspace = trueatomic_refcell = "0.1"allo-isolate = { version = "^0.1", features = ["catch-unwind"], optional = true }-futures = "0.3.30"+futures = "0.3.31"cfg-if = "1.0.0"futures-util = "0.3.30"@@ -36,7 +36,7 @@ base64 = { version = "0.22.1" }[dev-dependencies]rand = "0.8.5"-futures = "0.3.30"+futures = "0.3.31"[target.'cfg(not(target_arch = "wasm32"))'.dependencies]zip = { version = "2.2.0", features = ["deflate"] }