Skip to content

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

chore: implement local ai config

Nathan815bb11cde82
diff --git a/frontend/appflowy_flutter/lib/ai/service/ai_input_control.dart b/frontend/appflowy_flutter/lib/ai/service/ai_input_control.dart
index 8e4b42d1ce..c468fdd6e9 100644
--- a/frontend/appflowy_flutter/lib/ai/service/ai_input_control.dart
+++ b/frontend/appflowy_flutter/lib/ai/service/ai_input_control.dart
@@ -14,7 +14,7 @@ class AIModelStateNotifier {
: _isDesktop = UniversalPlatform.isDesktop,
_localAIListener =
UniversalPlatform.isDesktop ? LocalAIStateListener() : null,
- _aiModelSwitchListener = AIModelSwitchListener(chatId: objectId);
+ _aiModelSwitchListener = AIModelSwitchListener(objectId: objectId);
final String objectId;
final bool _isDesktop;
diff --git a/frontend/appflowy_flutter/lib/mobile/presentation/home/mobile_home_setting_page.dart b/frontend/appflowy_flutter/lib/mobile/presentation/home/mobile_home_setting_page.dart
index b5845f763e..09e22fc746 100644
--- a/frontend/appflowy_flutter/lib/mobile/presentation/home/mobile_home_setting_page.dart
+++ b/frontend/appflowy_flutter/lib/mobile/presentation/home/mobile_home_setting_page.dart
@@ -100,7 +100,6 @@ class _MobileHomeSettingPageState extends State<MobileHomeSettingPage> {
key: ValueKey(currentWorkspaceId),
userProfile: userProfile,
workspaceId: currentWorkspaceId,
- currentWorkspaceMemberRole: state.currentWorkspace?.role,
),
const SupportSettingGroup(),
const AboutSettingGroup(),
diff --git a/frontend/appflowy_flutter/lib/mobile/presentation/setting/ai/ai_settings_group.dart b/frontend/appflowy_flutter/lib/mobile/presentation/setting/ai/ai_settings_group.dart
index 3b75b08543..b43ada6e42 100644
--- a/frontend/appflowy_flutter/lib/mobile/presentation/setting/ai/ai_settings_group.dart
+++ b/frontend/appflowy_flutter/lib/mobile/presentation/setting/ai/ai_settings_group.dart
@@ -5,8 +5,6 @@ import 'package:appflowy/mobile/presentation/setting/widgets/mobile_setting_item
import 'package:appflowy/mobile/presentation/widgets/flowy_option_tile.dart';
import 'package:appflowy/workspace/application/settings/ai/settings_ai_bloc.dart';
import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pb.dart';
-import 'package:appflowy_backend/protobuf/flowy-user/workspace.pbenum.dart';
-import 'package:collection/collection.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra_ui/style_widget/text.dart';
import 'package:flutter/material.dart';
@@ -18,12 +16,10 @@ class AiSettingsGroup extends StatelessWidget {
super.key,
required this.userProfile,
required this.workspaceId,
- this.currentWorkspaceMemberRole,
});
final UserProfilePB userProfile;
final String workspaceId;
- final AFRolePB? currentWorkspaceMemberRole;
@override
Widget build(BuildContext context) {
@@ -32,7 +28,6 @@ class AiSettingsGroup extends StatelessWidget {
create: (context) => SettingsAIBloc(
userProfile,
workspaceId,
- currentWorkspaceMemberRole,
)..add(const SettingsAIEvent.started()),
child: BlocBuilder<SettingsAIBloc, SettingsAIState>(
builder: (context, state) {
@@ -48,7 +43,7 @@ class AiSettingsGroup extends StatelessWidget {
children: [
Flexible(
child: FlowyText(
- state.selectedAIModel,
+ state.availableModels?.selectedModel.name ?? "",
color: theme.colorScheme.onSurface,
overflow: TextOverflow.ellipsis,
),
@@ -84,16 +79,19 @@ class AiSettingsGroup extends StatelessWidget {
title: LocaleKeys.settings_aiPage_keys_llmModelType.tr(),
builder: (_) {
return Column(
- children: availableModels
- .mapIndexed(
- (index, model) => FlowyOptionTile.checkbox(
- text: model.name,
- showTopBorder: index == 0,
- isSelected: state.selectedAIModel == model.name,
+ children: (availableModels?.models ?? [])
+ .asMap()
+ .entries
+ .map(
+ (entry) => FlowyOptionTile.checkbox(
+ text: entry.value.name,
+ showTopBorder: entry.key == 0,
+ isSelected:
+ availableModels?.selectedModel.name == entry.value.name,
onTap: () {
context
.read<SettingsAIBloc>()
- .add(SettingsAIEvent.selectModel(model.name));
+ .add(SettingsAIEvent.selectModel(entry.value));
context.pop();
},
),
diff --git a/frontend/appflowy_flutter/lib/plugins/ai_chat/application/ai_model_switch_listener.dart b/frontend/appflowy_flutter/lib/plugins/ai_chat/application/ai_model_switch_listener.dart
index f25ea4deca..2cfc349bf8 100644
--- a/frontend/appflowy_flutter/lib/plugins/ai_chat/application/ai_model_switch_listener.dart
+++ b/frontend/appflowy_flutter/lib/plugins/ai_chat/application/ai_model_switch_listener.dart
@@ -12,14 +12,14 @@ import 'package:appflowy_result/appflowy_result.dart';
typedef OnUpdateSelectedModel = void Function(AIModelPB model);
class AIModelSwitchListener {
- AIModelSwitchListener({required this.chatId}) {
- _parser = ChatNotificationParser(id: chatId, callback: _callback);
+ AIModelSwitchListener({required this.objectId}) {
+ _parser = ChatNotificationParser(id: objectId, callback: _callback);
_subscription = RustStreamReceiver.listen(
(observable) => _parser?.parse(observable),
);
}
- final String chatId;
+ final String objectId;
StreamSubscription<SubscribeObject>? _subscription;
ChatNotificationParser? _parser;
diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/settings_ai_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/settings_ai_bloc.dart
index 6dcd52b294..1d50cde480 100644
--- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/settings_ai_bloc.dart
+++ b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/settings_ai_bloc.dart
@@ -1,5 +1,5 @@
+import 'package:appflowy/plugins/ai_chat/application/ai_model_switch_listener.dart';
import 'package:appflowy/user/application/user_listener.dart';
-import 'package:appflowy/user/application/user_service.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart';
@@ -10,48 +10,38 @@ import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'settings_ai_bloc.freezed.dart';
-part 'settings_ai_bloc.g.dart';
class SettingsAIBloc extends Bloc<SettingsAIEvent, SettingsAIState> {
SettingsAIBloc(
this.userProfile,
this.workspaceId,
- AFRolePB? currentWorkspaceMemberRole,
) : _userListener = UserListener(userProfile: userProfile),
- _userService = UserBackendService(userId: userProfile.id),
+ _aiModelSwitchListener =
+ AIModelSwitchListener(objectId: "ai_models_global_active_model"),
super(
SettingsAIState(
- selectedAIModel: userProfile.aiModel,
userProfile: userProfile,
- currentWorkspaceMemberRole: currentWorkspaceMemberRole,
),
) {
+ _aiModelSwitchListener.start(
+ onUpdateSelectedModel: (model) {
+ if (!isClosed) {
+ _loadModelList();
+ }
+ },
+ );
_dispatch();
-
- if (currentWorkspaceMemberRole == null) {
- _userService.getWorkspaceMember().then((result) {
- result.fold(
- (member) {
- if (!isClosed) {
- add(SettingsAIEvent.refreshMember(member));
- }
- },
- (err) {
- Log.error(err);
- },
- );
- });
- }
}
final UserListener _userListener;
final UserProfilePB userProfile;
- final UserBackendService _userService;
final String workspaceId;
+ final AIModelSwitchListener _aiModelSwitchListener;
@override
Future<void> close() async {
await _userListener.stop();
+ await _aiModelSwitchListener.stop();
return super.close();
}
@@ -67,7 +57,6 @@ class SettingsAIBloc extends Bloc<SettingsAIEvent, SettingsAIState> {
}
},
);
- _loadUserWorkspaceSetting();
_loadModelList();
},
didReceiveUserProfile: (userProfile) {
@@ -82,38 +71,25 @@ class SettingsAIBloc extends Bloc<SettingsAIEvent, SettingsAIState> {
!(state.aiSettings?.disableSearchIndexing ?? false),
);
},
- selectModel: (String model) async {
- await _updateUserWorkspaceSetting(model: model);
+ selectModel: (AIModelPB model) async {
+ if (!model.isLocal) {
+ await _updateUserWorkspaceSetting(model: model.name);
+ }
},
didLoadAISetting: (UseAISettingPB settings) {
emit(
state.copyWith(
aiSettings: settings,
- selectedAIModel: settings.aiModel,
enableSearchIndexing: !settings.disableSearchIndexing,
),
);
},
- didLoadAvailableModels: (List<AvailableModelPB> models) {
- if (state.selectedAIModel.isEmpty) {
- final m = models.firstWhere((model) => model.isDefault);
- _updateUserWorkspaceSetting(model: m.name);
- emit(
- state.copyWith(
- availableModels: models,
- selectedAIModel: m.name,
- ),
- );
- } else {
- emit(
- state.copyWith(
- availableModels: models,
- ),
- );
- }
- },
- refreshMember: (member) {
- emit(state.copyWith(currentWorkspaceMemberRole: member.role));
+ didLoadAvailableModels: (AvailableModelsPB models) {
+ emit(
+ state.copyWith(
+ availableModels: models,
+ ),
+ );
},
);
});
@@ -148,24 +124,11 @@ class SettingsAIBloc extends Bloc<SettingsAIEvent, SettingsAIState> {
(err) => Log.error(err),
);
- void _loadUserWorkspaceSetting() {
- final payload = UserWorkspaceIdPB(workspaceId: workspaceId);
- UserEventGetWorkspaceSetting(payload).send().then((result) {
- result.fold((settings) {
- if (!isClosed) {
- add(SettingsAIEvent.didLoadAISetting(settings));
- }
- }, (err) {
- Log.error(err);
- });
- });
- }
-
void _loadModelList() {
AIEventGetServerAvailableModels().send().then((result) {
- result.fold((config) {
+ result.fold((models) {
if (!isClosed) {
- add(SettingsAIEvent.didLoadAvailableModels(config.models));
+ add(SettingsAIEvent.didLoadAvailableModels(models));
}
}, (err) {
Log.error(err);
@@ -182,17 +145,15 @@ class SettingsAIEvent with _$SettingsAIEvent {
) = _DidLoadWorkspaceSetting;
const factory SettingsAIEvent.toggleAISearch() = _toggleAISearch;
- const factory SettingsAIEvent.refreshMember(WorkspaceMemberPB member) =
- _RefreshMember;
- const factory SettingsAIEvent.selectModel(String model) = _SelectAIModel;
+ const factory SettingsAIEvent.selectModel(AIModelPB model) = _SelectAIModel;
const factory SettingsAIEvent.didReceiveUserProfile(
UserProfilePB newUserProfile,
) = _DidReceiveUserProfile;
const factory SettingsAIEvent.didLoadAvailableModels(
- List<AvailableModelPB> models,
+ AvailableModelsPB models,
) = _DidLoadAvailableModels;
}
@@ -201,23 +162,7 @@ class SettingsAIState with _$SettingsAIState {
const factory SettingsAIState({
required UserProfilePB userProfile,
UseAISettingPB? aiSettings,
- @Default("Default") String selectedAIModel,
- AFRolePB? currentWorkspaceMemberRole,
- @Default([]) List<AvailableModelPB> availableModels,
+ AvailableModelsPB? availableModels,
@Default(true) bool enableSearchIndexing,
}) = _SettingsAIState;
}
-
-@JsonSerializable()
-class ModelList {
- ModelList({
- required this.models,
- });
-
- factory ModelList.fromJson(Map<String, dynamic> json) =>
- _$ModelListFromJson(json);
-
- final List<String> models;
-
- Map<String, dynamic> toJson() => _$ModelListToJson(this);
-}
diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/model_selection.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/model_selection.dart
index 8cd2f31114..bec4013a36 100644
--- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/model_selection.dart
+++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/model_selection.dart
@@ -1,3 +1,4 @@
+import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart';
import 'package:flutter/material.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
@@ -15,6 +16,10 @@ class AIModelSelection extends StatelessWidget {
Widget build(BuildContext context) {
return BlocBuilder<SettingsAIBloc, SettingsAIState>(
builder: (context, state) {
+ if (state.availableModels == null) {
+ return const SizedBox.shrink();
+ }
+
return Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: Row(
@@ -28,17 +33,17 @@ class AIModelSelection extends StatelessWidget {
),
const Spacer(),
Flexible(
- child: SettingsDropdown<String>(
+ child: SettingsDropdown<AIModelPB>(
key: const Key('_AIModelSelection'),
onChanged: (model) => context
.read<SettingsAIBloc>()
.add(SettingsAIEvent.selectModel(model)),
- selectedOption: state.selectedAIModel,
- options: state.availableModels
+ selectedOption: state.availableModels!.selectedModel,
+ options: state.availableModels!.models
.map(
- (model) => buildDropdownMenuEntry<String>(
+ (model) => buildDropdownMenuEntry<AIModelPB>(
context,
- value: model.name,
+ value: model,
label: model.name,
),
)
diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/settings_ai_view.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/settings_ai_view.dart
index 7102520ac1..0fc1ef293c 100644
--- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/settings_ai_view.dart
+++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/settings_ai_view.dart
@@ -42,9 +42,8 @@ class SettingsAIView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider<SettingsAIBloc>(
- create: (_) =>
- SettingsAIBloc(userProfile, workspaceId, currentWorkspaceMemberRole)
- ..add(const SettingsAIEvent.started()),
+ create: (_) => SettingsAIBloc(userProfile, workspaceId)
+ ..add(const SettingsAIEvent.started()),
child: BlocBuilder<SettingsAIBloc, SettingsAIState>(
builder: (context, state) {
final children = <Widget>[
diff --git a/frontend/rust-lib/Cargo.lock b/frontend/rust-lib/Cargo.lock
index 6b6af73c8f..ba07ff9c3b 100644
--- a/frontend/rust-lib/Cargo.lock
+++ b/frontend/rust-lib/Cargo.lock
@@ -198,7 +198,7 @@ dependencies = [
[[package]]
name = "appflowy-local-ai"
version = "0.1.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-LocalAI?rev=cd991507870d225e76df41164deee356cd9921a7#cd991507870d225e76df41164deee356cd9921a7"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-LocalAI?rev=52ad76f21f8f3a7b510dae029836b5fe86479e5a#52ad76f21f8f3a7b510dae029836b5fe86479e5a"
dependencies = [
"anyhow",
"appflowy-plugin",
@@ -218,7 +218,7 @@ dependencies = [
[[package]]
name = "appflowy-plugin"
version = "0.1.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-LocalAI?rev=cd991507870d225e76df41164deee356cd9921a7#cd991507870d225e76df41164deee356cd9921a7"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-LocalAI?rev=52ad76f21f8f3a7b510dae029836b5fe86479e5a#52ad76f21f8f3a7b510dae029836b5fe86479e5a"
dependencies = [
"anyhow",
"cfg-if",
diff --git a/frontend/rust-lib/Cargo.toml b/frontend/rust-lib/Cargo.toml
index a660ebcb5e..843a939ae3 100644
--- a/frontend/rust-lib/Cargo.toml
+++ b/frontend/rust-lib/Cargo.toml
@@ -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 = "cd991507870d225e76df41164deee356cd9921a7" }
-appflowy-plugin = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "cd991507870d225e76df41164deee356cd9921a7" }
+appflowy-local-ai = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "52ad76f21f8f3a7b510dae029836b5fe86479e5a" }
+appflowy-plugin = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "52ad76f21f8f3a7b510dae029836b5fe86479e5a" }
diff --git a/frontend/rust-lib/build-tool/flowy-codegen/src/ts_event/mod.rs b/frontend/rust-lib/build-tool/flowy-codegen/src/ts_event/mod.rs
index da8c05f360..97a7f5f529 100644
--- a/frontend/rust-lib/build-tool/flowy-codegen/src/ts_event/mod.rs
+++ b/frontend/rust-lib/build-tool/flowy-codegen/src/ts_event/mod.rs
@@ -153,7 +153,6 @@ pub fn parse_event_crate(event_crate: &TsEventCrate) -> Vec<EventASTContext> {
attrs
.iter()
.filter(|attr| !attr.attrs.event_attrs.ignore)
- .into_iter()
.map(|variant| EventASTContext::from(&variant.attrs))
.collect::<Vec<_>>()
},
diff --git a/frontend/rust-lib/flowy-ai-pub/src/cloud.rs b/frontend/rust-lib/flowy-ai-pub/src/cloud.rs
index 4639a93501..29cef4afde 100644
--- a/frontend/rust-lib/flowy-ai-pub/src/cloud.rs
+++ b/frontend/rust-lib/flowy-ai-pub/src/cloud.rs
@@ -29,10 +29,26 @@ pub struct AIModel {
pub is_local: bool,
}
+impl AIModel {
+ pub fn server(name: String) -> Self {
+ Self {
+ name,
+ is_local: false,
+ }
+ }
+
+ pub fn local(name: String) -> Self {
+ Self {
+ name,
+ is_local: true,
+ }
+ }
+}
+
impl Default for AIModel {
fn default() -> Self {
Self {
- name: "default".to_string(),
+ name: "Auto".to_string(),
is_local: false,
}
}
diff --git a/frontend/rust-lib/flowy-ai/src/ai_manager.rs b/frontend/rust-lib/flowy-ai/src/ai_manager.rs
index 2486637ef8..03a18dcdec 100644
--- a/frontend/rust-lib/flowy-ai/src/ai_manager.rs
+++ b/frontend/rust-lib/flowy-ai/src/ai_manager.rs
@@ -27,7 +27,7 @@ use lib_infra::util::timestamp;
use std::path::PathBuf;
use std::sync::{Arc, Weak};
use tokio::sync::RwLock;
-use tracing::{error, info, trace};
+use tracing::{error, info, instrument, trace};
pub trait AIUserService: Send + Sync + 'static {
fn user_id(&self) -> Result<i64, FlowyError>;
@@ -62,6 +62,8 @@ struct ServerModelsCache {
timestamp: Option<i64>,
}
+pub const GLOBAL_ACTIVE_MODEL_KEY: &str = "global_active_model";
+
pub struct AIManager {
pub cloud_service_wm: Arc<AICloudServiceMiddleware>,
pub user_service: Arc<dyn AIUserService>,
@@ -266,7 +268,7 @@ impl AIManager {
Ok(())
}
- pub async fn get_workspace_select_model(&self) -> FlowyResult<String> {
+ async fn get_workspace_select_model(&self) -> FlowyResult<String> {
let workspace_id = self.user_service.workspace_id()?;
let model = self
.cloud_service_wm
@@ -275,7 +277,7 @@ impl AIManager {
Ok(model)
}
- pub async fn get_server_available_models(&self) -> FlowyResult<Vec<AvailableModel>> {
+ async fn get_server_available_models(&self) -> FlowyResult<Vec<AvailableModel>> {
let workspace_id = self.user_service.workspace_id()?;
let now = timestamp();
@@ -339,97 +341,108 @@ impl AIManager {
}
}
- pub async fn update_selected_model(&self, source: String, model: AIModelPB) -> FlowyResult<()> {
+ pub async fn update_selected_model(&self, source: String, model: AIModel) -> FlowyResult<()> {
let source_key = ai_available_models_key(&source);
self
.store_preferences
- .set_object::<AIModel>(&source_key, &model.clone().into())?;
+ .set_object::<AIModel>(&source_key, &model)?;
chat_notification_builder(&source, ChatNotification::DidUpdateSelectedModel)
- .payload(model)
+ .payload(AIModelPB::from(model))
.send();
Ok(())
}
+ #[instrument(skip_all, level = "debug")]
+ pub async fn toggle_local_ai(&self) -> FlowyResult<()> {
+ let enabled = self.local_ai.toggle_local_ai().await?;
+ let source_key = ai_available_models_key(GLOBAL_ACTIVE_MODEL_KEY);
+ if enabled {
+ if let Some(name) = self.local_ai.get_plugin_chat_model() {
+ info!("Set global active model to local ai: {}", name);
+ let model = AIModel::local(name);
+ self.update_selected_model(source_key, model).await?;
+ }
+ } else {
+ info!("Set global active model to default");
+ let global_active_model = self
+ .get_workspace_select_model()
+ .await
+ .map(AIModel::server)
+ .unwrap_or_else(|_| AIModel::default());
+
+ self
+ .update_selected_model(source_key, global_active_model)
+ .await?;
+ }
+
+ Ok(())
+ }
+
pub async fn get_available_models(&self, source: String) -> FlowyResult<AvailableModelsPB> {
// Build the models list from server models and mark them as non-local.
let mut models: Vec<AIModelPB> = self
.get_server_available_models()
.await?
.into_iter()
- .map(|m| AIModelPB {
- name: m.name,
- is_local: false,
- })
+ .map(|m| AIModelPB::server(m.name))
.collect();
- // Optionally add the local plugin model.
+ // If user enable local ai, then add local ai model to the list.
if let Some(local_model) = self.local_ai.get_plugin_chat_model() {
- models.push(AIModelPB {
- name: local_model,
- is_local: true,
- });
+ models.push(AIModelPB::local(local_model));
}
if models.is_empty() {
return Ok(AvailableModelsPB {
models,
- selected_model: None,
+ selected_model: AIModelPB::default(),
});
}
+ // Global active model is the model selected by the user in the workspace settings.
+ let global_active_model = self
+ .get_workspace_select_model()
+ .await
+ .map(AIModel::server)
+ .unwrap_or_else(|_| AIModel::default());
+
+ let mut user_selected_model = global_active_model.clone();
let source_key = ai_available_models_key(&source);
- // Retrieve stored selected model, if any.
- let stored_selected = self.store_preferences.get_object::<AIModel>(&source_key);
-
- // Get workspace default model once.
- let workspace_default = self.get_workspace_select_model().await.ok();
-
- // Determine the effective selected model.
- let effective_selected = stored_selected.unwrap_or_else(|| {
- if let Some(ws_name) = workspace_default.clone() {
- let model = AIModel {
- name: ws_name,
- is_local: false,
- };
- // Store the default if not present.
- let _ = self.store_preferences.set_object(&source_key, &model);
- model
- } else {
- AIModel::default()
- }
- });
+ // If source is provided, try to get the user-selected model from the store. User selected
+ // model will be used as the active model if it exists.
+ match self.store_preferences.get_object::<AIModel>(&source_key) {
+ None => {
+ // when there is selected model and current local ai is active, then use local ai
+ if let Some(local_ai_model) = models.iter().find(|m| m.is_local) {
+ user_selected_model = AIModel::from(local_ai_model.clone());
+ }
+ },
+ Some(model) => {
+ user_selected_model = model;
+ },
+ }
- // Find a matching model in the available list.
- let used_model = models
+ // If user selected model is not available in the list, use the global active model.
+ let active_model = models
.iter()
- .find(|m| m.name == effective_selected.name)
+ .find(|m| m.name == user_selected_model.name)
.cloned()
- .or_else(|| {
- // If no match, try to use the workspace default if available.
- if let Some(ws_name) = workspace_default {
- Some(AIModelPB {
- name: ws_name,
- is_local: false,
- })
- } else {
- models.first().cloned()
- }
- });
+ .or_else(|| Some(AIModelPB::from(global_active_model)));
// Update the stored preference if a different model is used.
- if let Some(ref used) = used_model {
- if used.name != effective_selected.name {
+ if let Some(ref active_model) = active_model {
+ if active_model.name != user_selected_model.name {
self
.store_preferences
- .set_object::<AIModel>(&source_key, &AIModel::from(used.clone()))?;
+ .set_object::<AIModel>(&source_key, &AIModel::from(active_model.clone()))?;
}
}
Ok(AvailableModelsPB {
models,
- selected_model: used_model,
+ selected_model: active_model.unwrap_or_default(),
})
}
diff --git a/frontend/rust-lib/flowy-ai/src/entities.rs b/frontend/rust-lib/flowy-ai/src/entities.rs
index ace7e3d807..0e996228c6 100644
--- a/frontend/rust-lib/flowy-ai/src/entities.rs
+++ b/frontend/rust-lib/flowy-ai/src/entities.rs
@@ -234,8 +234,8 @@ pub struct AvailableModelsPB {
#[pb(index = 1)]
pub models: Vec<AIModelPB>,
- #[pb(index = 2, one_of)]
- pub selected_model: Option<AIModelPB>,
+ #[pb(index = 2)]
+ pub selected_model: AIModelPB,
}
#[derive(Default, ProtoBuf, Clone, Debug)]
@@ -247,6 +247,22 @@ pub struct AIModelPB {
pub is_local: bool,
}
+impl AIModelPB {
+ pub fn server(name: String) -> Self {
+ Self {
+ name,
+ is_local: false,
+ }
+ }
+
+ pub fn local(name: String) -> Self {
+ Self {
+ name,
+ is_local: true,
+ }
+ }
+}
+
impl From<AIModel> for AIModelPB {
fn from(model: AIModel) -> Self {
Self {
diff --git a/frontend/rust-lib/flowy-ai/src/event_handler.rs b/frontend/rust-lib/flowy-ai/src/event_handler.rs
index 2bc182b9b1..6bedebd261 100644
--- a/frontend/rust-lib/flowy-ai/src/event_handler.rs
+++ b/frontend/rust-lib/flowy-ai/src/event_handler.rs
@@ -1,10 +1,13 @@
use std::fs;
use std::path::PathBuf;
-use crate::ai_manager::AIManager;
+use crate::ai_manager::{AIManager, GLOBAL_ACTIVE_MODEL_KEY};
use crate::completion::AICompletion;
use crate::entities::*;
-use flowy_ai_pub::cloud::{ChatMessageMetadata, ChatMessageType, ChatRAGData, ContextLoader};
+use crate::util::ai_available_models_key;
+use flowy_ai_pub::cloud::{
+ AIModel, ChatMessageMetadata, ChatMessageType, ChatRAGData, ContextLoader,
+};
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
use lib_dispatch::prelude::{data_result_ok, AFPluginData, AFPluginState, DataResult};
use std::sync::{Arc, Weak};
@@ -104,14 +107,11 @@ pub(crate) async fn regenerate_response_handler(
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn get_server_model_list_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
-) -> DataResult<ServerAvailableModelsPB, FlowyError> {
+) -> DataResult<AvailableModelsPB, FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
- let models = ai_manager.get_server_available_models().await?;
- let models = models
- .into_iter()
- .map(AvailableModelPB::from)
- .collect::<Vec<_>>();
- data_result_ok(ServerAvailableModelsPB { models })
+ let source_key = ai_available_models_key(GLOBAL_ACTIVE_MODEL_KEY);
+ let models = ai_manager.get_available_models(source_key).await?;
+ data_result_ok(models)
}
#[tracing::instrument(level = "debug", skip_all, err)]
@@ -132,7 +132,7 @@ pub(crate) async fn update_selected_model_handler(
let data = data.try_into_inner()?;
let ai_manager = upgrade_ai_manager(ai_manager)?;
ai_manager
- .update_selected_model(data.source, data.selected_model)
+ .update_selected_model(data.source, AIModel::from(data.selected_model))
.await?;
Ok(())
}
@@ -286,7 +286,7 @@ 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 _ = ai_manager.local_ai.toggle_local_ai().await?;
+ ai_manager.toggle_local_ai().await?;
let state = ai_manager.local_ai.get_local_ai_state().await;
data_result_ok(state)
}
diff --git a/frontend/rust-lib/flowy-ai/src/event_map.rs b/frontend/rust-lib/flowy-ai/src/event_map.rs
index 84c2266f0b..0e24ca6a21 100644
--- a/frontend/rust-lib/flowy-ai/src/event_map.rs
+++ b/frontend/rust-lib/flowy-ai/src/event_map.rs
@@ -115,7 +115,7 @@ pub enum AIEvent {
#[event(input = "RegenerateResponsePB")]
RegenerateResponse = 27,
- #[event(output = "ServerAvailableModelsPB")]
+ #[event(output = "AvailableModelsPB")]
GetServerAvailableModels = 28,
#[event(output = "LocalAISettingPB")]
diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs b/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs
index 04d73831b9..503345e206 100644
--- a/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs
+++ b/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs
@@ -322,7 +322,6 @@ impl LocalAIController {
let enabled = !self.store_preferences.get_bool(&key).unwrap_or(true);
self.store_preferences.set_bool(&key, enabled)?;
self.toggle_plugin(enabled).await?;
-
Ok(enabled)
}
diff --git a/frontend/rust-lib/flowy-ai/src/util.rs b/frontend/rust-lib/flowy-ai/src/util.rs
index d1667ae98b..a181d1b1d3 100644
--- a/frontend/rust-lib/flowy-ai/src/util.rs
+++ b/frontend/rust-lib/flowy-ai/src/util.rs
@@ -1,3 +1,3 @@
pub fn ai_available_models_key(object_id: &str) -> String {
- format!("ai_available_models_{}", object_id)
+ format!("ai_models_{}", object_id)
}