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: refactor local ai (#7880)
Nathan.fooobdac73e934e5
diff --git a/.github/workflows/rust_ci.yaml b/.github/workflows/rust_ci.yamlindex 4ff78c689d..3ab5f91ba4 100644--- a/.github/workflows/rust_ci.yaml+++ b/.github/workflows/rust_ci.yaml@@ -106,7 +106,7 @@ jobs:af_cloud_test_ws_url: ws://localhost/ws/v1af_cloud_test_gotrue_url: http://localhost/gotruerun: |- DISABLE_CI_TEST_LOG="true" cargo test --no-default-features --features="dart"+ DISABLE_CI_TEST_LOG="true" cargo test --no-default-features --features="dart" -- --skip local_ollama_test- name: rustfmt rust-librun: cargo fmt --all -- --checkdiff --git a/frontend/appflowy_flutter/lib/ai/service/ai_model_state_notifier.dart b/frontend/appflowy_flutter/lib/ai/service/ai_model_state_notifier.dartindex 1d9ba5bf0e..85028855c4 100644--- a/frontend/appflowy_flutter/lib/ai/service/ai_model_state_notifier.dart+++ b/frontend/appflowy_flutter/lib/ai/service/ai_model_state_notifier.dart@@ -70,11 +70,6 @@ class AIModelStateNotifier {stateCallback: (state) async {_localAIState = state;_updateAll();- if (state.state == RunningStatePB.Running ||- state.state == RunningStatePB.Stopped) {- await _loadModelSelection();- _updateAll();- }},);}@@ -190,7 +185,7 @@ class AIModelStateNotifier {}final enabled = _localAIState!.enabled;- final running = _localAIState!.state == RunningStatePB.Running;+ final running = _localAIState!.isReady;final hintKey = enabled? (running? LocaleKeys.chat_inputLocalAIMessageHintdiff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_bloc.dartindex 492c19ab73..41b3b4e27f 100644--- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_bloc.dart+++ b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_bloc.dart@@ -39,8 +39,7 @@ class LocalAiPluginBloc extends Bloc<LocalAiPluginEvent, LocalAiPluginState> {emit(LocalAiPluginState.ready(isEnabled: aiState.enabled,- version: aiState.pluginVersion,- runningState: aiState.state,+ isReady: aiState.isReady,lackOfResource:aiState.hasLackOfResource() ? aiState.lackOfResource : null,),@@ -116,8 +115,7 @@ class LocalAiPluginState with _$LocalAiPluginState {const factory LocalAiPluginState.ready({required bool isEnabled,- required String version,- required RunningStatePB runningState,+ required bool isReady,required LackOfAIResourcePB? lackOfResource,}) = ReadyLocalAiPluginState;@@ -125,15 +123,15 @@ class LocalAiPluginState with _$LocalAiPluginState {bool get isEnabled {return maybeWhen(- ready: (isEnabled, _, __, ___) => isEnabled,+ ready: (isEnabled, _, ___) => isEnabled,orElse: () => false,);}bool get showIndicator {return maybeWhen(- ready: (isEnabled, _, runningState, lackOfResource) =>- runningState != RunningStatePB.Running || lackOfResource != null,+ ready: (isEnabled, isReady, lackOfResource) =>+ isReady || lackOfResource != null,orElse: () => false,);}diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_status_indicator.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_status_indicator.dartindex a280cf0644..e66ec4578d 100644--- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_status_indicator.dart+++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_status_indicator.dart@@ -19,19 +19,14 @@ class LocalAIStatusIndicator extends StatelessWidget {return BlocBuilder<LocalAiPluginBloc, LocalAiPluginState>(builder: (context, state) {return state.maybeWhen(- ready: (_, version, runningState, lackOfResource) {+ ready: (_, isReady, lackOfResource) {if (lackOfResource != null) {return _LackOfResource(resource: lackOfResource);}- return switch (runningState) {- RunningStatePB.ReadyToRun => const _ReadyToRun(),- RunningStatePB.Connecting ||- RunningStatePB.Connected =>- _Initializing(),- RunningStatePB.Running => _LocalAIRunning(version: version),- RunningStatePB.Stopped => const _RestartPluginButton(),- _ => const SizedBox.shrink(),+ return switch (isReady) {+ true => const _LocalAIRunning(),+ false => const _RestartPluginButton(),};},orElse: () => const SizedBox.shrink(),@@ -41,66 +36,6 @@ class LocalAIStatusIndicator extends StatelessWidget {}}-class _ReadyToRun extends StatelessWidget {- const _ReadyToRun();-- @override- Widget build(BuildContext context) {- return Padding(- padding: const EdgeInsets.symmetric(vertical: 8.0),- child: Row(- children: [- const SizedBox.square(- dimension: 20.0,- child: CircularProgressIndicator(- strokeWidth: 2.0,- strokeAlign: BorderSide.strokeAlignInside,- ),- ),- const HSpace(8.0),- Expanded(- child: FlowyText(- LocaleKeys.settings_aiPage_keys_localAIStart.tr(),- maxLines: 3,- overflow: TextOverflow.ellipsis,- ),- ),- ],- ),- );- }-}--class _Initializing extends StatelessWidget {- const _Initializing();-- @override- Widget build(BuildContext context) {- return Padding(- padding: const EdgeInsets.symmetric(vertical: 8.0),- child: Row(- children: [- const SizedBox.square(- dimension: 20.0,- child: CircularProgressIndicator(- strokeWidth: 2.0,- strokeAlign: BorderSide.strokeAlignInside,- ),- ),- HSpace(8),- Expanded(- child: FlowyText(- LocaleKeys.settings_aiPage_keys_localAIInitializing.tr(),- maxLines: 3,- overflow: TextOverflow.ellipsis,- ),- ),- ],- ),- );- }-}-class _RestartPluginButton extends StatelessWidget {const _RestartPluginButton();@@ -163,16 +98,11 @@ class _RestartPluginButton extends StatelessWidget {}class _LocalAIRunning extends StatelessWidget {- const _LocalAIRunning({- required this.version,- });-- final String version;+ const _LocalAIRunning();@overrideWidget build(BuildContext context) {final runningText = LocaleKeys.settings_aiPage_keys_localAIRunning.tr();- final text = version.isEmpty ? runningText : "$runningText ($version)";return Container(decoration: const BoxDecoration(@@ -189,7 +119,7 @@ class _LocalAIRunning extends StatelessWidget {const HSpace(6),Expanded(child: FlowyText(- text,+ runningText,color: const Color(0xFF1E4620),maxLines: 3,),diff --git a/frontend/appflowy_flutter/macos/Podfile.lock b/frontend/appflowy_flutter/macos/Podfile.lockindex 879f010c46..9709711e3b 100644--- a/frontend/appflowy_flutter/macos/Podfile.lock+++ b/frontend/appflowy_flutter/macos/Podfile.lock@@ -135,32 +135,32 @@ EXTERNAL SOURCES::path: Flutter/ephemeral/.symlinks/plugins/window_manager/macosSPEC CHECKSUMS:- app_links: 9028728e32c83a0831d9db8cf91c526d16cc5468- appflowy_backend: 464aeb3e5c6966a41641a2111e5ead72ce2695f7- auto_updater_macos: 3a42f1a06be6981f1a18be37e6e7bf86aa732118- bitsdojo_window_macos: 7959fb0ca65a3ccda30095c181ecb856fae48ea9- connectivity_plus: e74b9f74717d2d99d45751750e266e55912baeb5- desktop_drop: e0b672a7d84c0a6cbc378595e82cdb15f2970a43- device_info_plus: 4fb280989f669696856f8b129e4a5e3cd6c48f76- file_selector_macos: 6280b52b459ae6c590af5d78fc35c7267a3c4b31- flowy_infra_ui: 8760ff42a789de40bf5007a5f176b454722a341e+ app_links: 10e0a0ab602ffaf34d142cd4862f29d34b303b2a+ appflowy_backend: 865496343de667fc8c600e04b9fd05234e130cf9+ auto_updater_macos: 3e3462c418fe4e731917eacd8d28eef7af84086d+ bitsdojo_window_macos: 44e3b8fe3dd463820e0321f6256c5b1c16bb6a00+ connectivity_plus: 18d3c32514c886e046de60e9c13895109866c747+ desktop_drop: 69eeff437544aa619c8db7f4481b3a65f7696898+ device_info_plus: 1b14eed9bf95428983aed283a8d51cce3d8c4215+ file_selector_macos: cc3858c981fe6889f364731200d6232dac1d812d+ flowy_infra_ui: 03301a39ad118771adbf051a664265c61c507f38FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24HotKey: 400beb7caa29054ea8d864c96f5ba7e5b4852277- hotkey_manager: b443f35f4d772162937aa73fd8995e579f8ac4e2- irondash_engine_context: 893c7d96d20ce361d7e996f39d360c4c2f9869ba- local_notifier: ebf072651e35ae5e47280ad52e2707375cb2ae4e- package_info_plus: f0052d280d17aa382b932f399edf32507174e870- path_provider_foundation: 080d55be775b7414fd5a5ef3ac137b97b097e564+ hotkey_manager: c32bf0bfe8f934b7bc17ab4ad5c4c142960b023c+ irondash_engine_context: da62996ee25616d2f01bbeb85dc115d813359478+ local_notifier: e9506bc66fc70311e8bc7291fb70f743c081e4ff+ package_info_plus: 12f1c5c2cfe8727ca46cbd0b26677728972d9a5b+ path_provider_foundation: 2b6b4c569c0fb62ec74538f866245ac84301af46ReachabilitySwift: 32793e867593cfc1177f5d16491e3a197d2fccda- screen_retriever_macos: 452e51764a9e1cdb74b3c541238795849f21557f- share_plus: 510bf0af1a42cd602274b4629920c9649c52f4cc- shared_preferences_foundation: 9e1978ff2562383bd5676f64ec4e9aa8fa06a6f7+ screen_retriever_macos: 776e0fa5d42c6163d2bf772d22478df4b302b161+ share_plus: 1fa619de8392a4398bfaf176d441853922614e89+ shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78Sparkle: 9c328bdcfbcaf8f030c4b678eadfd0fcb03822d8- sqflite_darwin: 20b2a3a3b70e43edae938624ce550a3cbf66a3d0- super_native_extensions: c2795d6d9aedf4a79fae25cb6160b71b50549189- url_launcher_macos: 0fba8ddabfc33ce0a9afe7c5fef5aab3d8d2d673- webview_flutter_wkwebview: 44d4dee7d7056d5ad185d25b38404436d56c547c- window_manager: e8d0b1431ab6c454f2b5c9ae26004bbfa43469aa+ sqflite_darwin: 5a7236e3b501866c1c9befc6771dfd73ffb8702d+ super_native_extensions: 85efee3a7495b46b04befcfc86ed12069264ebf3+ url_launcher_macos: c82c93949963e55b228a30115bd219499a6fe404+ webview_flutter_wkwebview: 0982481e3d9c78fd5c6f62a002fcd24fc791f1e4+ window_manager: 990c8e348c4da2a93b81da638245d40554ec9436PODFILE CHECKSUM: 0532f3f001ca3110b8be345d6491fff690e95823diff --git a/frontend/rust-lib/Cargo.lock b/frontend/rust-lib/Cargo.lockindex a145257de5..1ec2d90640 100644--- a/frontend/rust-lib/Cargo.lock+++ b/frontend/rust-lib/Cargo.lock@@ -493,7 +493,7 @@ checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f"[[package]]name = "app-error"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=bfe9da67254f4e17a9cb05ffb88bd4a586a71179#bfe9da67254f4e17a9cb05ffb88bd4a586a71179"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=667bb51f57f63ec02137053be71e77236f56faa7#667bb51f57f63ec02137053be71e77236f56faa7"dependencies = ["anyhow","bincode",@@ -513,7 +513,7 @@ dependencies = [[[package]]name = "appflowy-ai-client"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=bfe9da67254f4e17a9cb05ffb88bd4a586a71179#bfe9da67254f4e17a9cb05ffb88bd4a586a71179"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=667bb51f57f63ec02137053be71e77236f56faa7#667bb51f57f63ec02137053be71e77236f56faa7"dependencies = ["anyhow","bytes",@@ -608,8 +608,8 @@ dependencies = ["futures","rand 0.8.5","reqwest 0.11.27",- "reqwest-eventsource",- "secrecy",+ "reqwest-eventsource 0.4.0",+ "secrecy 0.8.0","serde","serde_json","thiserror 1.0.64",@@ -619,6 +619,43 @@ dependencies = ["tracing",]+[[package]]+name = "async-openai"+version = "0.28.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "14d76e2f5af19477d6254415acc95ba97c6cc6f3b1e3cb4676b7f0fab8194298"+dependencies = [+ "async-openai-macros",+ "backoff",+ "base64 0.22.1",+ "bytes",+ "derive_builder 0.20.2",+ "eventsource-stream",+ "futures",+ "rand 0.8.5",+ "reqwest 0.12.15",+ "reqwest-eventsource 0.6.0",+ "secrecy 0.10.3",+ "serde",+ "serde_json",+ "thiserror 2.0.12",+ "tokio",+ "tokio-stream",+ "tokio-util",+ "tracing",+]++[[package]]+name = "async-openai-macros"+version = "0.1.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "0289cba6d5143bfe8251d57b4a8cac036adf158525a76533a7082ba65ec76398"+dependencies = [+ "proc-macro2",+ "quote",+ "syn 2.0.94",+]+[[package]]name = "async-recursion"version = "1.1.1"@@ -899,6 +936,15 @@ dependencies = ["generic-array",]+[[package]]+name = "block-padding"+version = "0.3.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "a8894febbff9f758034a5b8e12d87918f56dfc64a8e1fe757d65e29041538d93"+dependencies = [+ "generic-array",+]+[[package]]name = "bon"version = "3.5.2"@@ -976,6 +1022,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "4c2f7349907b712260e64b0afe2f84692af14a454be26187d9df565c7f69266a"dependencies = ["memchr",+ "regex-automata 0.3.9","serde",]@@ -1007,6 +1054,12 @@ dependencies = ["syn 1.0.109",]+[[package]]+name = "bytecount"+version = "0.6.8"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5ce89b21cab1437276d2650d57e971f9d548a2d9037cc231abdc0562b97498ce"+[[package]]name = "byteorder"version = "1.4.3"@@ -1052,6 +1105,15 @@ dependencies = ["pkg-config",]+[[package]]+name = "cbc"+version = "0.1.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "26b52a9543ae338f279b96b0b9fed9c8093744685043739079ce85cd58f289a6"+dependencies = [+ "cipher",+]+[[package]]name = "cc"version = "1.1.10"@@ -1074,7 +1136,7 @@ version = "0.6.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"dependencies = [- "nom",+ "nom 7.1.3",][[package]]@@ -1171,7 +1233,7 @@ dependencies = [[[package]]name = "client-api"version = "0.2.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=bfe9da67254f4e17a9cb05ffb88bd4a586a71179#bfe9da67254f4e17a9cb05ffb88bd4a586a71179"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=667bb51f57f63ec02137053be71e77236f56faa7#667bb51f57f63ec02137053be71e77236f56faa7"dependencies = ["again","anyhow",@@ -1226,7 +1288,7 @@ dependencies = [[[package]]name = "client-api-entity"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=bfe9da67254f4e17a9cb05ffb88bd4a586a71179#bfe9da67254f4e17a9cb05ffb88bd4a586a71179"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=667bb51f57f63ec02137053be71e77236f56faa7#667bb51f57f63ec02137053be71e77236f56faa7"dependencies = ["collab-entity","collab-rt-entity",@@ -1239,7 +1301,7 @@ dependencies = [[[package]]name = "client-websocket"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=bfe9da67254f4e17a9cb05ffb88bd4a586a71179#bfe9da67254f4e17a9cb05ffb88bd4a586a71179"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=667bb51f57f63ec02137053be71e77236f56faa7#667bb51f57f63ec02137053be71e77236f56faa7"dependencies = ["futures-channel","futures-util",@@ -1510,7 +1572,7 @@ dependencies = [[[package]]name = "collab-rt-entity"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=bfe9da67254f4e17a9cb05ffb88bd4a586a71179#bfe9da67254f4e17a9cb05ffb88bd4a586a71179"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=667bb51f57f63ec02137053be71e77236f56faa7#667bb51f57f63ec02137053be71e77236f56faa7"dependencies = ["anyhow","bincode",@@ -1532,7 +1594,7 @@ dependencies = [[[package]]name = "collab-rt-protocol"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=bfe9da67254f4e17a9cb05ffb88bd4a586a71179#bfe9da67254f4e17a9cb05ffb88bd4a586a71179"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=667bb51f57f63ec02137053be71e77236f56faa7#667bb51f57f63ec02137053be71e77236f56faa7"dependencies = ["anyhow","async-trait",@@ -1563,6 +1625,15 @@ dependencies = ["tracing",]+[[package]]+name = "colored"+version = "3.0.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "fde0e0ec90c9dfb3b4b1a0891a7dcd0e2bffde2f7efed5fe7c9bb00e5bfb915e"+dependencies = [+ "windows-sys 0.59.0",+]+[[package]]name = "concurrent-queue"version = "2.5.0"@@ -1802,6 +1873,19 @@ name = "cssparser"version = "0.31.2"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "5b3df4f93e5fbbe73ec01ec8d3f68bba73107993a5b1e7519273c32db9b0d5be"+dependencies = [+ "cssparser-macros",+ "dtoa-short",+ "itoa",+ "phf 0.8.0",+ "smallvec",+]++[[package]]+name = "cssparser"+version = "0.34.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b7c66d1cd8ed61bf80b38432613a7a2f09401ab8d0501110655f8b341484a3e3"dependencies = ["cssparser-macros","dtoa-short",@@ -1989,7 +2073,7 @@ checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308"[[package]]name = "database-entity"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=bfe9da67254f4e17a9cb05ffb88bd4a586a71179#bfe9da67254f4e17a9cb05ffb88bd4a586a71179"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=667bb51f57f63ec02137053be71e77236f56faa7#667bb51f57f63ec02137053be71e77236f56faa7"dependencies = ["bincode","bytes",@@ -2283,12 +2367,27 @@ version = "1.0.13"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "bbfc4744c1b8f2a09adc0e55242f60b1af195d88596bd8700be74418c056c555"+[[package]]+name = "ecb"+version = "0.1.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "1a8bfa975b1aec2145850fcaa1c6fe269a16578c44705a532ae3edc92b8881c7"+dependencies = [+ "cipher",+]+[[package]]name = "ego-tree"version = "0.6.2"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "3a68a4904193147e0a8dec3314640e6db742afd5f6e634f428a6af230d9b3591"+[[package]]+name = "ego-tree"+version = "0.9.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "7c6ba7d4eec39eaa9ab24d44a0e73a7949a1095a8b3f3abb11eddf27dbb56a53"+[[package]]name = "either"version = "1.15.0"@@ -2422,7 +2521,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "74fef4569247a5f429d9156b9d0a2599914385dd189c539334c625d8099d90ab"dependencies = ["futures-core",- "nom",+ "nom 7.1.3","pin-project-lite",]@@ -2479,6 +2578,16 @@ dependencies = ["regex",]+[[package]]+name = "fancy-regex"+version = "0.12.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "7493d4c459da9f84325ad297371a6b2b8a162800873a22e3b6b6512e61d18c05"+dependencies = [+ "bit-set",+ "regex",+]+[[package]]name = "fancy-regex"version = "0.13.0"@@ -2486,8 +2595,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "531e46835a22af56d1e3b66f04844bed63158bc094a628bec1d321d9b4c44bf2"dependencies = ["bit-set",- "regex-automata 0.4.7",- "regex-syntax 0.8.4",+ "regex-automata 0.4.9",+ "regex-syntax 0.8.5",][[package]]@@ -2555,11 +2664,14 @@ dependencies = ["allo-isolate","anyhow","arc-swap",+ "async-stream",+ "async-trait","base64 0.21.5","bytes","dashmap 6.0.1","flowy-ai-pub","flowy-codegen",+ "flowy-database-pub","flowy-derive","flowy-error","flowy-notification",@@ -2568,24 +2680,31 @@ dependencies = ["flowy-storage-pub","futures","futures-util",+ "langchain-rust","lib-dispatch","lib-infra","log",+ "lopdf","notify","ollama-rs",+ "once_cell","pin-project","protobuf",+ "pulldown-cmark 0.13.0","reqwest 0.11.27","schemars","serde","serde_json","sha2","strum_macros 0.21.1",- "text-splitter",+ "tempdir",+ "tempfile",+ "text-splitter 0.25.1","tokio","tokio-stream","tokio-util","tracing",+ "tracing-subscriber","twox-hash","uuid","validator 0.18.1",@@ -2737,7 +2856,7 @@ dependencies = ["flowy-error","flowy-notification","futures",- "indexmap 2.1.0",+ "indexmap 2.9.0","lazy_static","lib-dispatch","lib-infra",@@ -2811,7 +2930,7 @@ dependencies = ["flowy-storage-pub","futures","getrandom 0.2.10",- "indexmap 2.1.0",+ "indexmap 2.9.0","lib-dispatch","lib-infra","nanoid",@@ -2966,7 +3085,7 @@ dependencies = ["protobuf","serde","serde_json",- "strum_macros 0.26.1",+ "strum_macros 0.26.4","tantivy","tokio","tokio-stream",@@ -3067,6 +3186,8 @@ dependencies = ["chrono","flowy-ai-pub","include_dir",+ "r2d2",+ "r2d2_sqlite","rusqlite","rusqlite_migration","serde_json",@@ -3256,6 +3377,12 @@ dependencies = ["libc",]+[[package]]+name = "fuchsia-cprng"+version = "0.1.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"+[[package]]name = "funty"version = "2.0.0"@@ -3442,6 +3569,18 @@ dependencies = ["wasm-bindgen",]+[[package]]+name = "getrandom"+version = "0.3.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0"+dependencies = [+ "cfg-if",+ "libc",+ "r-efi",+ "wasi 0.14.2+wasi-0.2.4",+]+[[package]]name = "ghash"version = "0.5.0"@@ -3504,7 +3643,7 @@ dependencies = [[[package]]name = "gotrue"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=bfe9da67254f4e17a9cb05ffb88bd4a586a71179#bfe9da67254f4e17a9cb05ffb88bd4a586a71179"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=667bb51f57f63ec02137053be71e77236f56faa7#667bb51f57f63ec02137053be71e77236f56faa7"dependencies = ["anyhow","getrandom 0.2.10",@@ -3519,7 +3658,7 @@ dependencies = [[[package]]name = "gotrue-entity"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=bfe9da67254f4e17a9cb05ffb88bd4a586a71179#bfe9da67254f4e17a9cb05ffb88bd4a586a71179"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=667bb51f57f63ec02137053be71e77236f56faa7#667bb51f57f63ec02137053be71e77236f56faa7"dependencies = ["app-error","jsonwebtoken",@@ -3559,7 +3698,7 @@ dependencies = ["futures-core","futures-sink","http 1.2.0",- "indexmap 2.1.0",+ "indexmap 2.9.0","slab","tokio","tokio-util",@@ -3612,7 +3751,7 @@ dependencies = ["base64 0.13.1","byteorder","flate2",- "nom",+ "nom 7.1.3","num-traits",]@@ -3667,6 +3806,15 @@ dependencies = ["windows-sys 0.48.0",]+[[package]]+name = "html-escape"+version = "0.2.13"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6d1ad449764d627e22bfd7cd5e8868264fc9236e07c752972b4080cd351cb476"+dependencies = [+ "utf8-width",+]+[[package]]name = "html5ever"version = "0.26.0"@@ -3675,12 +3823,24 @@ checksum = "bea68cab48b8459f17cf1c944c67ddc572d272d9f2b274140f223ecb1da4a3b7"dependencies = ["log","mac",- "markup5ever",+ "markup5ever 0.11.0","proc-macro2","quote","syn 1.0.109",]+[[package]]+name = "html5ever"+version = "0.29.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "3b7410cae13cbc75623c98ac4cbfd1f0bedddf3227afc24f370cf0f50a44a11c"+dependencies = [+ "log",+ "mac",+ "markup5ever 0.14.1",+ "match_token",+]+[[package]]name = "htmlescape"version = "0.3.1"@@ -3807,6 +3967,7 @@ dependencies = ["http 1.2.0","http-body 1.0.1","httparse",+ "httpdate","itoa","pin-project-lite","smallvec",@@ -3839,6 +4000,7 @@ dependencies = ["hyper 1.5.2","hyper-util","rustls 0.23.20",+ "rustls-native-certs 0.8.0","rustls-pki-types","tokio","tokio-rustls 0.26.1",@@ -4197,19 +4359,19 @@ dependencies = [[[package]]name = "indexmap"-version = "2.1.0"+version = "2.9.0"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"+checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e"dependencies = ["equivalent",- "hashbrown 0.14.3",+ "hashbrown 0.15.2","serde",][[package]]name = "infra"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=bfe9da67254f4e17a9cb05ffb88bd4a586a71179#bfe9da67254f4e17a9cb05ffb88bd4a586a71179"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=667bb51f57f63ec02137053be71e77236f56faa7#667bb51f57f63ec02137053be71e77236f56faa7"dependencies = ["anyhow","bytes",@@ -4249,6 +4411,7 @@ version = "0.1.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5"dependencies = [+ "block-padding","generic-array",]@@ -4296,6 +4459,15 @@ dependencies = ["either",]+[[package]]+name = "itertools"+version = "0.13.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"+dependencies = [+ "either",+]+[[package]]name = "itertools"version = "0.14.0"@@ -4311,6 +4483,35 @@ version = "1.0.9"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"+[[package]]+name = "jiff"+version = "0.2.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "3590fea8e9e22d449600c9bbd481a8163bef223e4ff938e5f55899f8cf1adb93"+dependencies = [+ "jiff-tzdb-platform",+ "log",+ "portable-atomic",+ "portable-atomic-util",+ "serde",+ "windows-sys 0.59.0",+]++[[package]]+name = "jiff-tzdb"+version = "0.1.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c1283705eb0a21404d2bfd6eef2a7593d240bc42a0bdb39db0ad6fa2ec026524"++[[package]]+name = "jiff-tzdb-platform"+version = "0.1.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "875a5a69ac2bab1a891711cf5eccbec1ce0341ea805560dcd90b7a2e925132e8"+dependencies = [+ "jiff-tzdb",+]+[[package]]name = "jobserver"version = "0.1.32"@@ -4364,6 +4565,41 @@ dependencies = ["libc",]+[[package]]+name = "langchain-rust"+version = "4.6.0"+source = "git+https://github.com/appflowy/langchain-rust?branch=bump_ollama_rs#f57eef4cbd124db06a40e757bc3cea68d73e169a"+dependencies = [+ "async-openai 0.28.1",+ "async-recursion",+ "async-stream",+ "async-trait",+ "csv",+ "futures",+ "futures-util",+ "glob",+ "html-escape",+ "log",+ "mockito",+ "ollama-rs",+ "readability",+ "regex",+ "reqwest 0.12.15",+ "reqwest-eventsource 0.6.0",+ "scraper 0.21.0",+ "secrecy 0.10.3",+ "serde",+ "serde_json",+ "strum_macros 0.27.1",+ "text-splitter 0.17.1",+ "thiserror 2.0.12",+ "tiktoken-rs",+ "tokio",+ "tokio-stream",+ "url",+ "urlencoding",+]+[[package]]name = "language-tags"version = "0.3.2"@@ -4577,6 +4813,36 @@ version = "0.4.21"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"+[[package]]+name = "lopdf"+version = "0.36.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "59fa2559e99ba0f26a12458aabc754432c805bbb8cba516c427825a997af1fb7"+dependencies = [+ "aes",+ "bitflags 2.9.0",+ "cbc",+ "chrono",+ "ecb",+ "encoding_rs",+ "flate2",+ "indexmap 2.9.0",+ "itoa",+ "jiff",+ "log",+ "md-5",+ "nom 8.0.0",+ "nom_locate",+ "rand 0.9.1",+ "rangemap",+ "rayon",+ "sha2",+ "stringprep",+ "thiserror 2.0.12",+ "time",+ "weezl",+]+[[package]]name = "lru"version = "0.12.3"@@ -4689,6 +4955,43 @@ dependencies = ["tendril",]+[[package]]+name = "markup5ever"+version = "0.14.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c7a7213d12e1864c0f002f52c2923d4556935a43dec5e71355c2760e0f6e7a18"+dependencies = [+ "log",+ "phf 0.11.2",+ "phf_codegen 0.11.2",+ "string_cache",+ "string_cache_codegen",+ "tendril",+]++[[package]]+name = "markup5ever_rcdom"+version = "0.2.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b9521dd6750f8e80ee6c53d65e2e4656d7de37064f3a7a5d2d11d05df93839c2"+dependencies = [+ "html5ever 0.26.0",+ "markup5ever 0.11.0",+ "tendril",+ "xml5ever",+]++[[package]]+name = "match_token"+version = "0.1.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "88a9689d8d44bf9964484516275f5cd4c9b59457a6940c1d5d0ecbb94510a36b"+dependencies = [+ "proc-macro2",+ "quote",+ "syn 2.0.94",+]+[[package]]name = "matchers"version = "0.1.0"@@ -4715,7 +5018,7 @@ dependencies = ["actix-web-lab","actix-ws","anyhow",- "async-openai",+ "async-openai 0.19.1","async-trait","bytes","bytestring",@@ -4738,6 +5041,16 @@ dependencies = ["uuid",]+[[package]]+name = "md-5"+version = "0.10.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"+dependencies = [+ "cfg-if",+ "digest",+]+[[package]]name = "md5"version = "0.7.0"@@ -4859,6 +5172,30 @@ dependencies = ["windows-sys 0.52.0",]+[[package]]+name = "mockito"+version = "1.7.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "7760e0e418d9b7e5777c0374009ca4c93861b9066f18cb334a20ce50ab63aa48"+dependencies = [+ "assert-json-diff",+ "bytes",+ "colored",+ "futures-util",+ "http 1.2.0",+ "http-body 1.0.1",+ "http-body-util",+ "hyper 1.5.2",+ "hyper-util",+ "log",+ "rand 0.9.1",+ "regex",+ "serde_json",+ "serde_urlencoded",+ "similar 2.2.1",+ "tokio",+]+[[package]]name = "moka"version = "0.12.8"@@ -4939,17 +5276,37 @@ dependencies = [][[package]]-name = "notify"-version = "6.1.1"+name = "nom"+version = "8.0.0"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "6205bd8bb1e454ad2e27422015fb5e4f2bcc7e08fa8f27058670d208324a4d2d"+checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405"dependencies = [- "bitflags 2.9.0",- "crossbeam-channel",- "filetime",- "fsevent-sys",- "inotify",- "kqueue",+ "memchr",+]++[[package]]+name = "nom_locate"+version = "5.0.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "0b577e2d69827c4740cba2b52efaad1c4cc7c73042860b199710b3575c68438d"+dependencies = [+ "bytecount",+ "memchr",+ "nom 8.0.0",+]++[[package]]+name = "notify"+version = "6.1.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6205bd8bb1e454ad2e27422015fb5e4f2bcc7e08fa8f27058670d208324a4d2d"+dependencies = [+ "bitflags 2.9.0",+ "crossbeam-channel",+ "filetime",+ "fsevent-sys",+ "inotify",+ "kqueue","libc","log","mio 0.8.9",@@ -5057,14 +5414,16 @@ dependencies = ["serde_json","static_assertions","thiserror 2.0.12",+ "tokio",+ "tokio-stream","url",][[package]]name = "once_cell"-version = "1.19.0"+version = "1.21.3"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"+checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"[[package]]name = "oneshot"@@ -5320,7 +5679,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9"dependencies = ["fixedbitset",- "indexmap 2.1.0",+ "indexmap 2.9.0",][[package]]@@ -5507,6 +5866,21 @@ dependencies = ["universal-hash",]+[[package]]+name = "portable-atomic"+version = "1.11.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e"++[[package]]+name = "portable-atomic-util"+version = "0.2.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507"+dependencies = [+ "portable-atomic",+]+[[package]]name = "powerfmt"version = "0.2.0"@@ -5812,6 +6186,36 @@ dependencies = ["psl-types",]+[[package]]+name = "pulldown-cmark"+version = "0.12.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "f86ba2052aebccc42cbbb3ed234b8b13ce76f75c3551a303cb2bcffcff12bb14"+dependencies = [+ "bitflags 2.9.0",+ "memchr",+ "unicase",+]++[[package]]+name = "pulldown-cmark"+version = "0.13.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "1e8bbe1a966bd2f362681a44f6edce3c2310ac21e4d5067a6e7ec396297a6ea0"+dependencies = [+ "bitflags 2.9.0",+ "getopts",+ "memchr",+ "pulldown-cmark-escape",+ "unicase",+]++[[package]]+name = "pulldown-cmark-escape"+version = "0.11.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae"+[[package]]name = "quanta"version = "0.12.3"@@ -5910,6 +6314,12 @@ dependencies = ["proc-macro2",]+[[package]]+name = "r-efi"+version = "5.2.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5"+[[package]]name = "r2d2"version = "0.8.10"@@ -5921,12 +6331,36 @@ dependencies = ["scheduled-thread-pool",]+[[package]]+name = "r2d2_sqlite"+version = "0.28.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "8998443b32daee2ad6f528afb19ad77c4a8acc4d8d55b3e5072ed42862fe261a"+dependencies = [+ "r2d2",+ "rusqlite",+ "uuid",+]+[[package]]name = "radium"version = "0.7.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"+[[package]]+name = "rand"+version = "0.4.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"+dependencies = [+ "fuchsia-cprng",+ "libc",+ "rand_core 0.3.1",+ "rdrand",+ "winapi",+]+[[package]]name = "rand"version = "0.7.3"@@ -5952,6 +6386,16 @@ dependencies = ["rand_core 0.6.4",]+[[package]]+name = "rand"+version = "0.9.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97"+dependencies = [+ "rand_chacha 0.9.0",+ "rand_core 0.9.3",+]+[[package]]name = "rand_chacha"version = "0.2.2"@@ -5972,6 +6416,31 @@ dependencies = ["rand_core 0.6.4",]+[[package]]+name = "rand_chacha"+version = "0.9.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"+dependencies = [+ "ppv-lite86",+ "rand_core 0.9.3",+]++[[package]]+name = "rand_core"+version = "0.3.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"+dependencies = [+ "rand_core 0.4.2",+]++[[package]]+name = "rand_core"+version = "0.4.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"+[[package]]name = "rand_core"version = "0.5.1"@@ -5990,6 +6459,15 @@ dependencies = ["getrandom 0.2.10",]+[[package]]+name = "rand_core"+version = "0.9.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"+dependencies = [+ "getrandom 0.3.2",+]+[[package]]name = "rand_distr"version = "0.4.3"@@ -6018,6 +6496,12 @@ dependencies = ["rand_core 0.5.1",]+[[package]]+name = "rangemap"+version = "1.5.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "f60fcc7d6849342eff22c4350c8b9a989ee8ceabc4b481253e8946b9fe83d684"+[[package]]name = "raw-cpuid"version = "11.1.0"@@ -6047,6 +6531,29 @@ dependencies = ["crossbeam-utils",]+[[package]]+name = "rdrand"+version = "0.4.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"+dependencies = [+ "rand_core 0.3.1",+]++[[package]]+name = "readability"+version = "0.3.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e56596e20a6d3cf715182d9b6829220621e6e985cec04d00410cee29821b4220"+dependencies = [+ "html5ever 0.26.0",+ "lazy_static",+ "markup5ever_rcdom",+ "regex",+ "reqwest 0.11.27",+ "url",+]+[[package]]name = "redox_syscall"version = "0.1.57"@@ -6082,14 +6589,14 @@ dependencies = [[[package]]name = "regex"-version = "1.9.5"+version = "1.11.1"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47"+checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"dependencies = ["aho-corasick","memchr",- "regex-automata 0.3.8",- "regex-syntax 0.7.5",+ "regex-automata 0.4.9",+ "regex-syntax 0.8.5",][[package]]@@ -6103,24 +6610,19 @@ dependencies = [[[package]]name = "regex-automata"-version = "0.3.8"+version = "0.3.9"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795"-dependencies = [- "aho-corasick",- "memchr",- "regex-syntax 0.7.5",-]+checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9"[[package]]name = "regex-automata"-version = "0.4.7"+version = "0.4.9"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"+checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"dependencies = ["aho-corasick","memchr",- "regex-syntax 0.8.4",+ "regex-syntax 0.8.5",][[package]]@@ -6137,15 +6639,18 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"[[package]]name = "regex-syntax"-version = "0.7.5"+version = "0.8.5"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da"+checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"[[package]]-name = "regex-syntax"-version = "0.8.4"+name = "remove_dir_all"+version = "0.5.3"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"+checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"+dependencies = [+ "winapi",+][[package]]name = "rend"@@ -6183,7 +6688,7 @@ dependencies = ["percent-encoding","pin-project-lite","rustls 0.21.7",- "rustls-native-certs",+ "rustls-native-certs 0.6.3","rustls-pemfile 1.0.3","serde","serde_json",@@ -6235,6 +6740,7 @@ dependencies = ["pin-project-lite","quinn","rustls 0.23.20",+ "rustls-native-certs 0.8.0","rustls-pemfile 2.2.0","rustls-pki-types","serde",@@ -6267,12 +6773,28 @@ dependencies = ["futures-core","futures-timer","mime",- "nom",+ "nom 7.1.3","pin-project-lite","reqwest 0.11.27","thiserror 1.0.64",]+[[package]]+name = "reqwest-eventsource"+version = "0.6.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "632c55746dbb44275691640e7b40c907c16a2dc1a5842aa98aaec90da6ec6bde"+dependencies = [+ "eventsource-stream",+ "futures-core",+ "futures-timer",+ "mime",+ "nom 7.1.3",+ "pin-project-lite",+ "reqwest 0.12.15",+ "thiserror 1.0.64",+]+[[package]]name = "ring"version = "0.16.20"@@ -6491,6 +7013,19 @@ dependencies = ["security-framework",]+[[package]]+name = "rustls-native-certs"+version = "0.8.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "fcaf18a4f2be7326cd874a5fa579fae794320a0f388d365dca7e480e55f83f8a"+dependencies = [+ "openssl-probe",+ "rustls-pemfile 2.2.0",+ "rustls-pki-types",+ "schannel",+ "security-framework",+]+[[package]]name = "rustls-pemfile"version = "1.0.3"@@ -6636,12 +7171,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "c95a930e03325234c18c7071fd2b60118307e025d6fff3e12745ffbf63a3d29c"dependencies = ["ahash 0.8.11",- "cssparser",- "ego-tree",+ "cssparser 0.31.2",+ "ego-tree 0.6.2","getopts",- "html5ever",+ "html5ever 0.26.0","once_cell",- "selectors",+ "selectors 0.25.0","smallvec","tendril",]@@ -6653,12 +7188,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "585480e3719b311b78a573db1c9d9c4c1f8010c2dee4cc59c2efe58ea4dbc3e1"dependencies = ["ahash 0.8.11",- "cssparser",- "ego-tree",+ "cssparser 0.31.2",+ "ego-tree 0.6.2","getopts",- "html5ever",+ "html5ever 0.26.0","once_cell",- "selectors",+ "selectors 0.25.0",+ "tendril",+]++[[package]]+name = "scraper"+version = "0.21.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b0e749d29b2064585327af5038a5a8eb73aeebad4a3472e83531a436563f7208"+dependencies = [+ "ahash 0.8.11",+ "cssparser 0.34.0",+ "ego-tree 0.9.0",+ "getopts",+ "html5ever 0.29.1",+ "precomputed-hash",+ "selectors 0.26.0","tendril",]@@ -6688,6 +7239,16 @@ dependencies = ["zeroize",]+[[package]]+name = "secrecy"+version = "0.10.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e891af845473308773346dc847b2c23ee78fe442e0472ac50e22a18a93d3ae5a"+dependencies = [+ "serde",+ "zeroize",+]+[[package]]name = "security-framework"version = "2.9.2"@@ -6718,7 +7279,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "4eb30575f3638fc8f6815f448d50cb1a2e255b0897985c8c59f4d37b72a07b06"dependencies = ["bitflags 2.9.0",- "cssparser",+ "cssparser 0.31.2","derive_more","fxhash","log",@@ -6726,7 +7287,26 @@ dependencies = ["phf 0.10.1","phf_codegen 0.10.0","precomputed-hash",- "servo_arc",+ "servo_arc 0.3.0",+ "smallvec",+]++[[package]]+name = "selectors"+version = "0.26.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "fd568a4c9bb598e291a08244a5c1f5a8a6650bee243b5b0f8dbb3d9cc1d87fe8"+dependencies = [+ "bitflags 2.9.0",+ "cssparser 0.34.0",+ "derive_more",+ "fxhash",+ "log",+ "new_debug_unreachable",+ "phf 0.11.2",+ "phf_codegen 0.11.2",+ "precomputed-hash",+ "servo_arc 0.4.0","smallvec",]@@ -6788,7 +7368,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "9d2de91cf02bbc07cde38891769ccd5d4f073d22a40683aa4bc7a95781aaa2c4"dependencies = ["form_urlencoded",- "indexmap 2.1.0",+ "indexmap 2.9.0","itoa","ryu","serde",@@ -6844,7 +7424,7 @@ version = "0.9.27"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "3cc7a1570e38322cfe4154732e5110f887ea57e22b76f4bfd32b5bdd3368666c"dependencies = [- "indexmap 2.1.0",+ "indexmap 2.9.0","itoa","ryu","serde",@@ -6885,6 +7465,15 @@ dependencies = ["stable_deref_trait",]+[[package]]+name = "servo_arc"+version = "0.4.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ae65c4249478a2647db249fb43e23cec56a2c8974a427e7bd8cb5a1d0964921a"+dependencies = [+ "stable_deref_trait",+]+[[package]]name = "sha1"version = "0.10.6"@@ -6925,7 +7514,7 @@ dependencies = [[[package]]name = "shared-entity"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=bfe9da67254f4e17a9cb05ffb88bd4a586a71179#bfe9da67254f4e17a9cb05ffb88bd4a586a71179"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=667bb51f57f63ec02137053be71e77236f56faa7#667bb51f57f63ec02137053be71e77236f56faa7"dependencies = ["anyhow","app-error",@@ -7125,6 +7714,17 @@ dependencies = ["quote",]+[[package]]+name = "stringprep"+version = "0.1.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1"+dependencies = [+ "unicode-bidi",+ "unicode-normalization",+ "unicode-properties",+]+[[package]]name = "strsim"version = "0.10.0"@@ -7143,6 +7743,15 @@ version = "0.25.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125"+[[package]]+name = "strum"+version = "0.26.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06"+dependencies = [+ "strum_macros 0.26.4",+]+[[package]]name = "strum"version = "0.27.1"@@ -7179,11 +7788,11 @@ dependencies = [[[package]]name = "strum_macros"-version = "0.26.1"+version = "0.26.4"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "7a3417fc93d76740d974a01654a09777cb500428cc874ca9f45edfe0c4d4cd18"+checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be"dependencies = [- "heck 0.4.1",+ "heck 0.5.0","proc-macro2","quote","rustversion",@@ -7429,7 +8038,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "d60769b80ad7953d8a7b2c70cdfe722bbcdcac6bccc8ac934c40c034d866fc18"dependencies = ["byteorder",- "regex-syntax 0.8.4",+ "regex-syntax 0.8.5","utf8-ranges",]@@ -7439,7 +8048,7 @@ version = "0.24.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "e810cdeeebca57fc3f7bfec5f85fdbea9031b2ac9b990eb5ff49b371d52bbe6a"dependencies = [- "nom",+ "nom 7.1.3","serde","serde_json",]@@ -7484,6 +8093,16 @@ version = "1.0.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"+[[package]]+name = "tempdir"+version = "0.3.7"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"+dependencies = [+ "rand 0.4.6",+ "remove_dir_all",+]+[[package]]name = "tempfile"version = "3.12.0"@@ -7549,6 +8168,25 @@ dependencies = ["winapi",]+[[package]]+name = "text-splitter"+version = "0.17.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6de8ec9ff8e9192f77add3b37ec040808a8bd6567c0616972b2a8568d7269c0d"+dependencies = [+ "ahash 0.8.11",+ "auto_enums",+ "either",+ "itertools 0.13.0",+ "once_cell",+ "pulldown-cmark 0.12.2",+ "regex",+ "strum 0.26.3",+ "thiserror 1.0.64",+ "tiktoken-rs",+ "unicode-segmentation",+]+[[package]]name = "text-splitter"version = "0.25.1"@@ -7627,6 +8265,21 @@ dependencies = ["once_cell",]+[[package]]+name = "tiktoken-rs"+version = "0.5.9"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c314e7ce51440f9e8f5a497394682a57b7c323d0f4d0a6b1b13c429056e0e234"+dependencies = [+ "anyhow",+ "base64 0.21.5",+ "bstr",+ "fancy-regex 0.12.0",+ "lazy_static",+ "parking_lot 0.12.1",+ "rustc-hash 1.1.0",+]+[[package]]name = "time"version = "0.3.36"@@ -7777,9 +8430,9 @@ dependencies = [[[package]]name = "tokio-stream"-version = "0.1.15"+version = "0.1.17"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "267ac89e0bec6e691e5813911606935d77c476ff49024f98abcea3e7b15e37af"+checksum = "eca58d7bba4a75707817a2c44174253f9236b2d5fbd055602e9d5c07c139a047"dependencies = ["futures-core","pin-project-lite",@@ -7817,16 +8470,16 @@ dependencies = [[[package]]name = "tokio-util"-version = "0.7.11"+version = "0.7.15"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1"+checksum = "66a539a9ad6d5d281510d5bd368c973d636c02dbf8a67300bfb6b950696ad7df"dependencies = ["bytes","futures-core","futures-io","futures-sink","futures-util",- "hashbrown 0.14.3",+ "hashbrown 0.15.2","pin-project-lite","slab","tokio",@@ -7868,7 +8521,7 @@ version = "0.21.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1"dependencies = [- "indexmap 2.1.0",+ "indexmap 2.9.0","toml_datetime","winnow 0.5.30",]@@ -7879,7 +8532,7 @@ version = "0.22.12"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "d3328d4f68a705b2a4498da1d580585d39a6510f98318a2cec3018a7ec61ddef"dependencies = [- "indexmap 2.1.0",+ "indexmap 2.9.0","serde","serde_spanned","toml_datetime",@@ -8271,11 +8924,17 @@ dependencies = ["tinyvec",]+[[package]]+name = "unicode-properties"+version = "0.1.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e70f2a8b45122e719eb623c01822704c4e0907e7e426a05927e1a1cfff5b75d0"+[[package]]name = "unicode-segmentation"-version = "1.10.1"+version = "1.12.0"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"+checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"[[package]]name = "unicode-width"@@ -8323,6 +8982,12 @@ dependencies = ["serde",]+[[package]]+name = "urlencoding"+version = "2.1.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"+[[package]]name = "utf-8"version = "0.7.6"@@ -8341,6 +9006,12 @@ version = "1.0.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "7fcfc827f90e53a02eaef5e535ee14266c1d569214c6aa70133a624d8a3164ba"+[[package]]+name = "utf8-width"+version = "0.1.7"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "86bd8d4e895da8537e5315b8254664e6b769c4ff3db18321b297a1e7004392e3"+[[package]]name = "utf8_iter"version = "1.0.4"@@ -8354,6 +9025,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314"dependencies = ["getrandom 0.2.10",+ "rand 0.8.5","serde","sha1_smol","wasm-bindgen",@@ -8468,6 +9140,15 @@ version = "0.11.0+wasi-snapshot-preview1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"+[[package]]+name = "wasi"+version = "0.14.2+wasi-0.2.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"+dependencies = [+ "wit-bindgen-rt",+]+[[package]]name = "wasm-bindgen"version = "0.2.100"@@ -8614,6 +9295,12 @@ dependencies = ["rustls-pki-types",]+[[package]]+name = "weezl"+version = "0.1.8"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082"+[[package]]name = "which"version = "4.4.2"@@ -8961,10 +9648,19 @@ dependencies = ["windows-sys 0.59.0",]+[[package]]+name = "wit-bindgen-rt"+version = "0.39.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"+dependencies = [+ "bitflags 2.9.0",+]+[[package]]name = "workspace-template"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=bfe9da67254f4e17a9cb05ffb88bd4a586a71179#bfe9da67254f4e17a9cb05ffb88bd4a586a71179"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=667bb51f57f63ec02137053be71e77236f56faa7#667bb51f57f63ec02137053be71e77236f56faa7"dependencies = ["anyhow","async-trait",@@ -8974,7 +9670,7 @@ dependencies = ["collab-entity","collab-folder","getrandom 0.2.10",- "indexmap 2.1.0",+ "indexmap 2.9.0","nanoid","serde","serde_json",@@ -9014,6 +9710,17 @@ dependencies = ["rustix",]+[[package]]+name = "xml5ever"+version = "0.17.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4034e1d05af98b51ad7214527730626f019682d797ba38b51689212118d8e650"+dependencies = [+ "log",+ "mac",+ "markup5ever 0.11.0",+]+[[package]]name = "xz2"version = "0.1.7"@@ -9204,7 +9911,7 @@ dependencies = ["displaydoc","flate2","hmac",- "indexmap 2.1.0",+ "indexmap 2.9.0","lzma-rs","memchr","pbkdf2 0.12.2",diff --git a/frontend/rust-lib/Cargo.toml b/frontend/rust-lib/Cargo.tomlindex a98c24faf6..23f6c7c8bc 100644--- a/frontend/rust-lib/Cargo.toml+++ b/frontend/rust-lib/Cargo.toml@@ -82,7 +82,8 @@ uuid = { version = "1.5.0", features = ["serde", "v4", "v5"] }serde_repr = "0.1"futures = "0.3.31"tokio = "1.38.0"-tokio-stream = "0.1.14"+tokio-stream = "0.1.17"+ollama-rs = { version = "0.3.0" }async-trait = "0.1.81"chrono = { version = "0.4.31", default-features = false, features = ["clock"] }collab = { version = "0.2" }@@ -110,9 +111,9 @@ flowy-sqlite-vec = { path = "flowy-sqlite-vec" }# Run the script.add_workspace_members:# scripts/tool/update_client_api_rev.sh new_rev_id# ⚠️⚠️⚠️️-client-api = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "bfe9da67254f4e17a9cb05ffb88bd4a586a71179" }-client-api-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "bfe9da67254f4e17a9cb05ffb88bd4a586a71179" }-workspace-template = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "bfe9da67254f4e17a9cb05ffb88bd4a586a71179" }+client-api = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "667bb51f57f63ec02137053be71e77236f56faa7" }+client-api-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "667bb51f57f63ec02137053be71e77236f56faa7" }+workspace-template = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "667bb51f57f63ec02137053be71e77236f56faa7" }[profile.dev]opt-level = 0@@ -163,3 +164,5 @@ collab-importer = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlaf-local-ai = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "093d3f4916a7b924ac66b4f0a9d81cc5fcc72eaa" }af-plugin = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "093d3f4916a7b924ac66b4f0a9d81cc5fcc72eaa" }af-mcp = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "093d3f4916a7b924ac66b4f0a9d81cc5fcc72eaa" }++langchain-rust = { version = "4.6.0", git = "https://github.com/appflowy/langchain-rust", branch = "bump_ollama_rs" }\ No newline at end of filediff --git a/frontend/rust-lib/flowy-ai-pub/src/cloud.rs b/frontend/rust-lib/flowy-ai-pub/src/cloud.rsindex d8bfa19a24..63d2fd2b54 100644--- a/frontend/rust-lib/flowy-ai-pub/src/cloud.rs+++ b/frontend/rust-lib/flowy-ai-pub/src/cloud.rs@@ -153,14 +153,14 @@ pub trait ChatCloudService: Send + Sync + 'static {workspace_id: &Uuid,chat_id: &Uuid,message_id: i64,- ai_model: Option<AIModel>,+ ai_model: AIModel,) -> Result<RepeatedRelatedQuestion, FlowyError>;async fn stream_complete(&self,workspace_id: &Uuid,params: CompleteTextParams,- ai_model: Option<AIModel>,+ ai_model: AIModel,) -> Result<StreamComplete, FlowyError>;async fn embed_file(diff --git a/frontend/rust-lib/flowy-ai-pub/src/entities.rs b/frontend/rust-lib/flowy-ai-pub/src/entities.rsindex 9224379584..842cb68422 100644--- a/frontend/rust-lib/flowy-ai-pub/src/entities.rs+++ b/frontend/rust-lib/flowy-ai-pub/src/entities.rs@@ -2,7 +2,9 @@ use crate::cloud::workspace_dto::ViewIcon;use crate::cloud::CollabType;use twox_hash::xxhash64::Hasher;use uuid::Uuid;-+pub const SOURCE_ID: &str = "id";+pub const SOURCE: &str = "appflowy";+pub const SOURCE_NAME: &str = "document";pub struct EmbeddingRecord {pub workspace_id: Uuid,pub object_id: Uuid,diff --git a/frontend/rust-lib/flowy-ai-pub/src/persistence/chat_message_sql.rs b/frontend/rust-lib/flowy-ai-pub/src/persistence/chat_message_sql.rsindex 230e5761d2..110659fa00 100644--- a/frontend/rust-lib/flowy-ai-pub/src/persistence/chat_message_sql.rs+++ b/frontend/rust-lib/flowy-ai-pub/src/persistence/chat_message_sql.rs@@ -1,3 +1,4 @@+use crate::cloud::chat_dto::ChatAuthorType;use crate::cloud::MessageCursor;use client_api::entity::chat_dto::ChatMessage;use flowy_error::{FlowyError, FlowyResult};@@ -40,6 +41,19 @@ impl ChatMessageTable {}}+pub fn select_latest_user_message(+ mut conn: DBConnection,+ chat_id_val: &str,+ auth_type: ChatAuthorType,+) -> FlowyResult<ChatMessageTable> {+ dsl::chat_message_table+ .filter(chat_message_table::chat_id.eq(chat_id_val))+ .filter(chat_message_table::author_type.eq(auth_type as i64))+ .order(chat_message_table::created_at.desc())+ .first::<ChatMessageTable>(&mut *conn)+ .map_err(FlowyError::from)+}+pub fn update_chat_message_is_sync(mut conn: DBConnection,chat_id_val: &str,diff --git a/frontend/rust-lib/flowy-ai/Cargo.toml b/frontend/rust-lib/flowy-ai/Cargo.tomlindex fd11419f6e..ab385553bc 100644--- a/frontend/rust-lib/flowy-ai/Cargo.toml+++ b/frontend/rust-lib/flowy-ai/Cargo.toml@@ -33,7 +33,7 @@ log = "0.4.21"serde = { workspace = true, features = ["derive"] }serde_json = { workspace = true }anyhow = "1.0.86"-tokio-stream = "0.1.15"+tokio-stream.workspace = truetokio-util = { workspace = true, features = ["full"] }af-local-ai = { workspace = true }af-plugin = { workspace = true }@@ -43,24 +43,38 @@ base64 = "0.21.5"futures-util = "0.3.30"pin-project = "1.1.5"flowy-storage-pub = { workspace = true }-ollama-rs = "0.3.0"+ollama-rs.workspace = trueschemars = "0.8.22"twox-hash = { version = "2.1.0", features = ["xxhash64"] }+async-trait.workspace = true+async-stream = "0.3.6"+flowy-database-pub = { workspace = true }+once_cell = "1.21.3"+langchain-rust = { version = "4.6.0", features = ["ollama"] }[target.'cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))'.dependencies]notify = "6.1.1"af-mcp = { version = "0.1.0" }text-splitter = { version = "0.25.1" }flowy-sqlite-vec.workspace = true+lopdf = { version = "0.36.0", optional = true }+pulldown-cmark = { version = "0.13.0", optional = true }[dev-dependencies]uuid.workspace = true+tracing-subscriber = { version = "0.3.3", features = ["env-filter"] }+tempfile = "3.8.1"+[build-dependencies]flowy-codegen.workspace = true+tempdir = "0.3.7"[features]+default = []dart = ["flowy-codegen/dart", "flowy-notification/dart"]local_ai = []+file_reader = ["lopdf", "pulldown-cmark"]+ai-tool = []\ No newline at end of filediff --git a/frontend/rust-lib/flowy-ai/src/ai_manager.rs b/frontend/rust-lib/flowy-ai/src/ai_manager.rsindex 056dfeacf4..ae6a552f71 100644--- a/frontend/rust-lib/flowy-ai/src/ai_manager.rs+++ b/frontend/rust-lib/flowy-ai/src/ai_manager.rs@@ -76,11 +76,6 @@ impl AIManager {local_ai: Arc<LocalAIController>,) -> AIManager {let user_service = Arc::new(user_service);- let cloned_local_ai = local_ai.clone();- tokio::spawn(async move {- cloned_local_ai.observe_plugin_resource().await;- });-let external_service = Arc::new(query_service);let cloud_service_wm = Arc::new(ChatServiceMiddleware::new(user_service.clone(),@@ -118,14 +113,14 @@ impl AIManager {let is_enabled = self.local_ai.is_enabled_on_workspace(&workspace_id.to_string());- let is_running = self.local_ai.is_running();+ let is_ready = self.local_ai.is_ready().await;info!(- "[AI Manager] Reloading workspace: {}, is_local: {}, is_enabled: {}, is_running: {}",- workspace_id, is_local, is_enabled, is_running+ "[AI Manager] Reloading workspace: {}, is_local: {}, is_enabled: {}, is_ready: {}",+ workspace_id, is_local, is_enabled, is_ready);// Shutdown AI if it's running but shouldn't be (not enabled and not in local mode)- if is_running && !is_enabled && !is_local {+ if is_ready && !is_enabled && !is_local {info!("[AI Manager] Local AI is running but not enabled, shutting it down");let local_ai = self.local_ai.clone();tokio::spawn(async move {@@ -137,7 +132,7 @@ impl AIManager {}// Start AI if it's enabled but not running- if is_enabled && !is_running {+ if is_enabled && !is_ready {info!("[AI Manager] Local AI is enabled but not running, starting it now");let local_ai = self.local_ai.clone();tokio::spawn(async move {@@ -149,7 +144,7 @@ impl AIManager {}// Log status for other cases- if is_running {+ if is_ready {info!("[AI Manager] Local AI is already running");}}@@ -157,7 +152,8 @@ impl AIManager {async fn prepare_local_ai(&self, workspace_id: &Uuid) {self.local_ai- .reload_ollama_client(&workspace_id.to_string());+ .reload_ollama_client(&workspace_id.to_string())+ .await;self.model_control.lock()@@ -210,9 +206,15 @@ impl AIManager {self.cloud_service_wm.clone(),))});- if self.local_ai.is_running() {++ if self.local_ai.is_ready().await {+ let workspace_id = self.user_service.workspace_id()?;+ let model = self.get_active_model(&chat_id.to_string()).await;trace!("[AI Plugin] notify open chat: {}", chat_id);- self.local_ai.open_chat(chat_id);+ self+ .local_ai+ .open_chat(&workspace_id, chat_id, &model.name)+ .await?;}let user_service = self.user_service.clone();@@ -255,11 +257,7 @@ impl AIManager {pub async fn delete_chat(&self, chat_id: &Uuid) -> Result<(), FlowyError> {if let Some((_, chat)) = self.chats.remove(chat_id) {chat.close();-- if self.local_ai.is_running() {- info!("[AI Plugin] notify close chat: {}", chat_id);- self.local_ai.close_chat(chat_id);- }+ self.local_ai.close_chat(chat_id);}Ok(())}@@ -353,8 +351,7 @@ impl AIManager {let workspace_id = self.user_service.workspace_id()?;let old_settings = self.local_ai.get_local_ai_setting();// Only restart if the server URL has changed and local AI is not running- let need_restart =- old_settings.ollama_server_url != setting.ollama_server_url && !self.local_ai.is_running();+ let need_restart = old_settings.ollama_server_url != setting.ollama_server_url;// Update settings firstself@@ -375,7 +372,8 @@ impl AIManager {if need_restart {self.local_ai- .reload_ollama_client(&workspace_id.to_string());+ .reload_ollama_client(&workspace_id.to_string())+ .await;self.local_ai.restart_plugin().await;}@@ -428,7 +426,7 @@ impl AIManager {if enabled {self.prepare_local_ai(&workspace_id).await;- if let Some(name) = self.local_ai.get_plugin_chat_model() {+ if let Some(name) = self.local_ai.get_local_chat_model() {let model = AIModel::local(name, "".to_string());info!("[Model Selection] Set global active model to local ai: {}",@@ -601,9 +599,7 @@ impl AIManager {) -> Result<RepeatedRelatedQuestionPB, FlowyError> {let chat = self.get_or_create_chat_instance(chat_id).await?;let ai_model = self.get_active_model(&chat_id.to_string()).await;- let resp = chat- .get_related_question(message_id, Some(ai_model))- .await?;+ let resp = chat.get_related_question(message_id, ai_model).await?;Ok(resp)}diff --git a/frontend/rust-lib/flowy-ai/src/ai_tool/markdown.rs b/frontend/rust-lib/flowy-ai/src/ai_tool/markdown.rsnew file mode 100644index 0000000000..745c4aa727--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/ai_tool/markdown.rs@@ -0,0 +1,32 @@+use pulldown_cmark::{html, Options, Parser};+use std::fs;++pub struct MdRead {+ path: String,+}++impl MdRead {+ /// Creates a new MdRead for the given file path.+ pub fn new<P: Into<String>>(path: P) -> Self {+ MdRead { path: path.into() }+ }++ pub fn read_markdown(&self) -> std::io::Result<String> {+ fs::read_to_string(&self.path)+ }++ /// Renders the markdown to HTML, with CommonMark extensions enabled.+ pub fn render_html(&self) -> Result<String, Box<dyn std::error::Error>> {+ let markdown_input = self.read_markdown()?;+ // Set up parser options (tables, footnotes, strikethrough)+ let mut options = Options::empty();+ options.insert(Options::ENABLE_TABLES);+ options.insert(Options::ENABLE_FOOTNOTES);+ options.insert(Options::ENABLE_STRIKETHROUGH);+ // Create parser and render HTML+ let parser = Parser::new_ext(&markdown_input, options);+ let mut html_output = String::with_capacity(markdown_input.len() * 3 / 2);+ html::push_html(&mut html_output, parser);+ Ok(html_output)+ }+}diff --git a/frontend/rust-lib/flowy-ai/src/ai_tool/mod.rs b/frontend/rust-lib/flowy-ai/src/ai_tool/mod.rsnew file mode 100644index 0000000000..5a66779c22--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/ai_tool/mod.rs@@ -0,0 +1,2 @@+pub mod markdown;+pub mod pdf;diff --git a/frontend/rust-lib/flowy-ai/src/ai_tool/pdf.rs b/frontend/rust-lib/flowy-ai/src/ai_tool/pdf.rsnew file mode 100644index 0000000000..8b570ccaf4--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/ai_tool/pdf.rs@@ -0,0 +1,31 @@+use lopdf::Document;++pub struct PdfRead {+ path: String,+}++impl PdfRead {+ pub fn new<P: Into<String>>(path: P) -> Self {+ PdfRead { path: path.into() }+ }++ /// Loads the PDF and extracts text from all pages, concatenated with double newlines.+ pub fn read_all(&self) -> lopdf::Result<String> {+ let doc = Document::load(&self.path)?;+ // Get a map of page numbers to object IDs+ let pages = doc.get_pages();+ let mut texts = Vec::with_capacity(pages.len());++ // Iterate pages in ascending order+ let mut page_numbers: Vec<u32> = pages.keys().cloned().collect();+ page_numbers.sort_unstable();+ for page_num in page_numbers {+ // Extract text for this page+ let page_text = doc.extract_text(&[page_num])?;+ texts.push(page_text);+ }++ // Join all pages with blank line separators+ Ok(texts.join("\n\n"))+ }+}diff --git a/frontend/rust-lib/flowy-ai/src/chat.rs b/frontend/rust-lib/flowy-ai/src/chat.rsindex 052599ef48..e594c66750 100644--- a/frontend/rust-lib/flowy-ai/src/chat.rs+++ b/frontend/rust-lib/flowy-ai/src/chat.rs@@ -152,7 +152,6 @@ impl Chat {self.stream_buffer.lock().await.clear();let format = format.map(Into::into).unwrap_or_default();-let answer_stream_buffer = self.stream_buffer.clone();let uid = self.user_service.user_id()?;let workspace_id = self.user_service.workspace_id()?;@@ -503,7 +502,7 @@ impl Chat {pub async fn get_related_question(&self,message_id: i64,- ai_model: Option<AIModel>,+ ai_model: AIModel,) -> Result<RepeatedRelatedQuestionPB, FlowyError> {let workspace_id = self.user_service.workspace_id()?;let resp = selfdiff --git a/frontend/rust-lib/flowy-ai/src/completion.rs b/frontend/rust-lib/flowy-ai/src/completion.rsindex ffdccd0680..f739e2ee95 100644--- a/frontend/rust-lib/flowy-ai/src/completion.rs+++ b/frontend/rust-lib/flowy-ai/src/completion.rs@@ -40,7 +40,7 @@ impl AICompletion {pub async fn create_complete_task(&self,complete: CompleteTextPB,- preferred_model: Option<AIModel>,+ preferred_model: AIModel,) -> FlowyResult<CompleteTextTaskPB> {if matches!(complete.completion_type, CompletionTypePB::CustomPrompt)&& complete.custom_prompt.is_none()@@ -84,14 +84,14 @@ pub struct CompletionTask {stop_rx: tokio::sync::mpsc::Receiver<()>,context: CompleteTextPB,cloud_service: Weak<dyn ChatCloudService>,- preferred_model: Option<AIModel>,+ preferred_model: AIModel,}impl CompletionTask {pub fn new(workspace_id: Uuid,context: CompleteTextPB,- preferred_model: Option<AIModel>,+ preferred_model: AIModel,cloud_service: Weak<dyn ChatCloudService>,stop_rx: tokio::sync::mpsc::Receiver<()>,) -> Self {@@ -117,7 +117,7 @@ impl CompletionTask {CompletionTypePB::MakeLonger => CompletionType::MakeLonger,CompletionTypePB::ContinueWriting => CompletionType::ContinueWriting,CompletionTypePB::ExplainSelected => CompletionType::Explain,- CompletionTypePB::UserQuestion => CompletionType::UserQuestion,+ CompletionTypePB::UserQuestion => CompletionType::AskAI,CompletionTypePB::CustomPrompt => CompletionType::CustomPrompt,};diff --git a/frontend/rust-lib/flowy-ai/src/embeddings/context.rs b/frontend/rust-lib/flowy-ai/src/embeddings/context.rsindex e1d9162502..2a1fb45cd4 100644--- a/frontend/rust-lib/flowy-ai/src/embeddings/context.rs+++ b/frontend/rust-lib/flowy-ai/src/embeddings/context.rs@@ -26,6 +26,10 @@ impl EmbedContext {})}+ pub fn get_vector_db(&self) -> Option<Arc<VectorSqliteDB>> {+ self.vector_db.load_full()+ }+pub fn init_vector_db(&self, db_path: PathBuf) {let sys = get_operating_system();if !sys.is_desktop() {diff --git a/frontend/rust-lib/flowy-ai/src/embeddings/document_indexer.rs b/frontend/rust-lib/flowy-ai/src/embeddings/document_indexer.rsindex bedd055494..8b122ce947 100644--- a/frontend/rust-lib/flowy-ai/src/embeddings/document_indexer.rs+++ b/frontend/rust-lib/flowy-ai/src/embeddings/document_indexer.rs@@ -1,6 +1,6 @@use crate::embeddings::embedder::Embedder;use crate::embeddings::indexer::{EmbeddingModel, Indexer};-use flowy_ai_pub::entities::EmbeddedChunk;+use flowy_ai_pub::entities::{EmbeddedChunk, SOURCE, SOURCE_ID, SOURCE_NAME};use flowy_error::FlowyError;use lib_infra::async_trait::async_trait;use ollama_rs::generation::embeddings::request::{EmbeddingsInput, GenerateEmbeddingsRequest};@@ -102,9 +102,9 @@ pub fn split_text_into_chunks(}let split_contents = group_paragraphs_by_max_content_len(paragraphs, chunk_size, overlap);let metadata = json!({- "id": object_id,- "source": "appflowy",- "name": "document",+ SOURCE_ID: object_id,+ SOURCE: "appflowy",+ SOURCE_NAME: "document",});let mut seen = std::collections::HashSet::new();diff --git a/frontend/rust-lib/flowy-ai/src/embeddings/mod.rs b/frontend/rust-lib/flowy-ai/src/embeddings/mod.rsindex abbe1cd443..d447d89958 100644--- a/frontend/rust-lib/flowy-ai/src/embeddings/mod.rs+++ b/frontend/rust-lib/flowy-ai/src/embeddings/mod.rs@@ -1,5 +1,6 @@pub mod context;mod document_indexer;mod embedder;-mod indexer;+pub(crate) mod indexer;mod scheduler;+pub mod store;diff --git a/frontend/rust-lib/flowy-ai/src/embeddings/scheduler.rs b/frontend/rust-lib/flowy-ai/src/embeddings/scheduler.rsindex 327ecda58d..b350499b23 100644--- a/frontend/rust-lib/flowy-ai/src/embeddings/scheduler.rs+++ b/frontend/rust-lib/flowy-ai/src/embeddings/scheduler.rs@@ -110,7 +110,7 @@ impl EmbeddingScheduler {Some(query_embed) => {let result = self.vector_db- .search_with_score(&workspace_id.to_string(), query_embed, 10, 0.4)+ .search_with_score(&workspace_id.to_string(), vec![], query_embed, 10, 0.4).await.map_err(|err| {error!("[Embedding] Failed to search: {}", err);diff --git a/frontend/rust-lib/flowy-ai/src/embeddings/store.rs b/frontend/rust-lib/flowy-ai/src/embeddings/store.rsnew file mode 100644index 0000000000..c01d5fac28--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/embeddings/store.rs@@ -0,0 +1,243 @@+use crate::embeddings::document_indexer::split_text_into_chunks;+use crate::embeddings::embedder::{Embedder, OllamaEmbedder};+use crate::embeddings::indexer::{EmbeddingModel, IndexerProvider};+use async_trait::async_trait;+use flowy_ai_pub::cloud::CollabType;+use flowy_ai_pub::entities::SOURCE_ID;+use flowy_error::FlowyError;+use flowy_sqlite_vec::db::VectorSqliteDB;+use futures::stream::{self, StreamExt};+use langchain_rust::llm::client::OllamaClient;+use langchain_rust::{+ schemas::Document,+ vectorstore::{VecStoreOptions, VectorStore},+};+use ollama_rs::generation::embeddings::request::{EmbeddingsInput, GenerateEmbeddingsRequest};+use serde_json::Value;+use std::collections::HashMap;+use std::error::Error;+use std::sync::{Arc, Weak};+use tracing::{error, trace};+use uuid::Uuid;++#[derive(Clone)]+pub struct SqliteVectorStore {+ ollama: Weak<OllamaClient>,+ vector_db: Weak<VectorSqliteDB>,+ indexer_provider: Arc<IndexerProvider>,+}++impl SqliteVectorStore {+ pub fn new(ollama: Weak<OllamaClient>, vector_db: Weak<VectorSqliteDB>) -> Self {+ Self {+ ollama,+ vector_db,+ indexer_provider: IndexerProvider::new(),+ }+ }++ pub(crate) fn create_embedder(&self) -> Result<Embedder, FlowyError> {+ let ollama = self+ .ollama+ .upgrade()+ .ok_or_else(|| FlowyError::internal().with_context("Ollama reference was dropped"))?;++ let embedder = Embedder::Ollama(OllamaEmbedder { ollama });+ Ok(embedder)+ }+}++#[async_trait]+impl VectorStore for SqliteVectorStore {+ type Options = VecStoreOptions<Value>;+ async fn add_documents(+ &self,+ docs: &[Document],+ _opt: &Self::Options,+ ) -> Result<Vec<String>, Box<dyn Error>> {+ let vector_db = match self.vector_db.upgrade() {+ Some(db) => db,+ None => return Err("Vector database not initialized".into()),+ };++ let embedder = self.create_embedder()?;++ let indexer = self+ .indexer_provider+ .indexer_for(CollabType::Document)+ .ok_or_else(|| Box::<dyn Error>::from("Failed to get indexer for Document"))?;++ // Parse documents and filter out invalid ones early+ let documents = docs+ .iter()+ .filter_map(|v| {+ let workspace_id = match v.metadata.get("workspace_id") {+ Some(value) => value.as_str().and_then(|s| Uuid::parse_str(s).ok()),+ None => None,+ }?;++ let object_id = match v.metadata.get(SOURCE_ID) {+ Some(value) => value.as_str().and_then(|s| Uuid::parse_str(s).ok()),+ None => None,+ }?;++ Some((workspace_id, object_id, v.page_content.clone()))+ })+ .collect::<Vec<(Uuid, Uuid, String)>>();++ let concurrency_limit = 4;+ let document_ids = stream::iter(documents)+ .map(|(workspace_id, object_id, paragraph)| {+ // Clone values that need to be moved into the async block+ let object_id_str = object_id.to_string();+ let workspace_id_str = workspace_id.to_string();+ let vector_db_clone = vector_db.clone();+ let embedder_clone = embedder.clone();+ let indexer_clone = indexer.clone();++ async move {+ let chunks_result = split_text_into_chunks(+ &object_id_str,+ vec![paragraph],+ EmbeddingModel::NomicEmbedText,+ 2000,+ 200,+ );++ match chunks_result {+ Ok(chunks) => match indexer_clone.embed(&embedder_clone, chunks).await {+ Ok(chunks) => {+ if let Err(e) = vector_db_clone+ .upsert_collabs_embeddings(&workspace_id_str, &object_id_str, chunks)+ .await+ {+ error!(+ "[Embedding] Failed to upsert document `{}`: {}",+ object_id_str, e+ );+ return None;+ }++ Some(object_id_str)+ },+ Err(err) => {+ error!(+ "[Embedding] Failed to embed document `{}`: {}",+ object_id_str, err+ );+ None+ },+ },+ Err(err) => {+ error!(+ "[Embedding] Failed to split document `{}`: {}",+ object_id_str, err+ );+ None+ },+ }+ }+ })+ .buffer_unordered(concurrency_limit)+ .filter_map(|result| async move { result })+ .collect()+ .await;++ Ok(document_ids)+ }++ async fn similarity_search(+ &self,+ query: &str,+ limit: usize,+ opt: &Self::Options,+ ) -> Result<Vec<Document>, Box<dyn Error>> {+ // Extract rag_ids from filters+ let rag_ids = opt+ .filters+ .as_ref()+ .and_then(|filters| filters.get("rag_ids"))+ .and_then(|value| value.as_array())+ .map(|array| {+ array+ .iter()+ .filter_map(|item| item.as_str().map(String::from))+ .collect::<Vec<_>>()+ })+ .unwrap_or_default();++ // Extract workspace_id from filters+ let workspace_id = opt+ .filters+ .as_ref()+ .and_then(|filters| filters.get("workspace_id"))+ .and_then(|value| value.as_str())+ .and_then(|s| Uuid::parse_str(s).ok());++ // Return empty result if workspace_id is missing+ let workspace_id = match workspace_id {+ Some(id) => id.to_string(),+ None => return Ok(Vec::new()),+ };++ // Get the vector database+ let vector_db = match self.vector_db.upgrade() {+ Some(db) => db,+ None => return Err("Vector database not initialized".into()),+ };++ // Create embedder and generate embedding for query+ let embedder = self.create_embedder()?;+ let request = GenerateEmbeddingsRequest::new(+ embedder.model().name().to_string(),+ EmbeddingsInput::Single(query.to_string()),+ );++ let embedding = match embedder.embed(request).await {+ Ok(result) => result.embeddings,+ Err(e) => return Err(Box::new(e)),+ };++ if embedding.is_empty() {+ return Ok(Vec::new());+ }++ let score_threshold = opt.score_threshold.unwrap_or(0.4);+ let query_embedding = embedding.first().unwrap();++ // Perform similarity search in the database+ let results = vector_db+ .search_with_score(+ &workspace_id,+ rag_ids,+ query_embedding,+ limit as i32,+ score_threshold,+ )+ .await?;++ trace!(+ "[VectorStore] Found {} results for query: {}",+ results.len(),+ query+ );++ // Convert results to Documents+ let documents = results+ .into_iter()+ .map(|result| {+ let mut metadata = HashMap::new();++ if let Some(map) = result.metadata.as_ref().and_then(|v| v.as_object()) {+ for (key, value) in map {+ metadata.insert(key.clone(), value.clone());+ }+ }++ Document::new(result.content).with_metadata(metadata)+ })+ .collect();++ Ok(documents)+ }+}diff --git a/frontend/rust-lib/flowy-ai/src/entities.rs b/frontend/rust-lib/flowy-ai/src/entities.rsindex 9ffa2acf33..d4fcb15096 100644--- a/frontend/rust-lib/flowy-ai/src/entities.rs+++ b/frontend/rust-lib/flowy-ai/src/entities.rs@@ -590,13 +590,7 @@ pub struct LocalAIPB {pub lack_of_resource: Option<LackOfAIResourcePB>,#[pb(index = 3)]- pub state: RunningStatePB,-- #[pb(index = 4, one_of)]- pub plugin_version: Option<String>,-- #[pb(index = 5)]- pub plugin_downloaded: bool,+ pub is_ready: bool,}#[derive(Default, ProtoBuf, Validate, Clone, Debug)]diff --git a/frontend/rust-lib/flowy-ai/src/event_handler.rs b/frontend/rust-lib/flowy-ai/src/event_handler.rsindex bdeeb58e89..03a8830e84 100644--- a/frontend/rust-lib/flowy-ai/src/event_handler.rs+++ b/frontend/rust-lib/flowy-ai/src/event_handler.rs@@ -193,7 +193,7 @@ pub(crate) async fn start_complete_text_handler(let data = data.into_inner();let ai_manager = upgrade_ai_manager(ai_manager)?;let ai_model = ai_manager.get_active_model(&data.object_id).await;- let task = tools.create_complete_task(data, Some(ai_model)).await?;+ let task = tools.create_complete_task(data, ai_model).await?;data_result_ok(task)}diff --git a/frontend/rust-lib/flowy-ai/src/lib.rs b/frontend/rust-lib/flowy-ai/src/lib.rsindex fc8620c394..f2ea33765d 100644--- a/frontend/rust-lib/flowy-ai/src/lib.rs+++ b/frontend/rust-lib/flowy-ai/src/lib.rs@@ -10,8 +10,53 @@ pub mod local_ai;// #[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]// pub mod mcp;+#[cfg(feature = "ai-tool")]+mod ai_tool;#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]pub mod embeddings;++#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]+pub use embeddings::store::SqliteVectorStore;++#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]+pub use mock::SqliteVectorStore;++#[cfg(all(+ not(target_os = "windows"),+ not(target_os = "macos"),+ not(target_os = "linux")+))]+#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]+mod mock {+ use async_trait::async_trait;+ use langchain_rust::schemas::Document;+ use langchain_rust::vectorstore::{VecStoreOptions, VectorStore};+ use serde_json::Value;+ use std::error::Error;+ #[derive(Clone)]+ pub struct SqliteVectorStore;+ #[async_trait]+ impl VectorStore for SqliteVectorStore {+ type Options = VecStoreOptions<Value>;+ async fn add_documents(+ &self,+ docs: &[Document],+ _opt: &Self::Options,+ ) -> Result<Vec<String>, Box<dyn Error>> {+ Ok(vec![])+ }++ async fn similarity_search(+ &self,+ query: &str,+ limit: usize,+ opt: &Self::Options,+ ) -> Result<Vec<Document>, Box<dyn Error>> {+ Ok(vec![])+ }+ }+}+mod middleware;mod model_select;#[cfg(test)]diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/chat/conversation_chain.rs b/frontend/rust-lib/flowy-ai/src/local_ai/chat/conversation_chain.rsnew file mode 100644index 0000000000..aa7173a79c--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/chat/conversation_chain.rs@@ -0,0 +1,469 @@+use crate::local_ai::chat::llm::LLMOllama;+use async_stream::stream;+use async_trait::async_trait;+use flowy_ai_pub::entities::SOURCE_ID;+use futures::Stream;+use futures_util::{pin_mut, StreamExt};+use langchain_rust::chain::{+ Chain, ChainError, CondenseQuestionGeneratorChain, CondenseQuestionPromptBuilder,+ StuffDocumentBuilder, StuffQAPromptBuilder,+};+use langchain_rust::language_models::{GenerateResult, TokenUsage};+use langchain_rust::memory::SimpleMemory;+use langchain_rust::prompt::{FormatPrompter, PromptArgs};+use langchain_rust::schemas::{BaseMemory, Document, Message, Retriever, StreamData};+use langchain_rust::vectorstore::{VecStoreOptions, VectorStore};+use serde_json::{json, Value};+use std::collections::HashSet;+use std::error::Error;+use std::{collections::HashMap, pin::Pin, sync::Arc};+use tokio::sync::Mutex;++pub(crate) const DEFAULT_OUTPUT_KEY: &str = "output";+pub(crate) const DEFAULT_RESULT_KEY: &str = "generate_result";++const CONVERSATIONAL_RETRIEVAL_QA_DEFAULT_SOURCE_DOCUMENT_KEY: &str = "source_documents";+const CONVERSATIONAL_RETRIEVAL_QA_DEFAULT_GENERATED_QUESTION_KEY: &str = "generated_question";+const CONVERSATIONAL_RETRIEVAL_QA_DEFAULT_INPUT_KEY: &str = "question";++pub struct ConversationalRetrieverChain {+ pub(crate) ollama: LLMOllama,+ pub(crate) retriever: AFRetriever<Value>,+ pub memory: Arc<Mutex<dyn BaseMemory>>,+ pub(crate) combine_documents_chain: Box<dyn Chain>,+ pub(crate) condense_question_chain: Box<dyn Chain>,+ pub(crate) rephrase_question: bool,+ pub(crate) return_source_documents: bool,+ pub(crate) input_key: String,+ pub(crate) output_key: String,+}+impl ConversationalRetrieverChain {+ pub fn set_rag_ids(&mut self, new_rag_ids: Vec<String>) {+ let filters = self+ .retriever+ .options+ .filters+ .get_or_insert_with(|| json!({}));++ filters["rag_ids"] = json!(new_rag_ids);+ }++ pub fn add_rag_ids<I, S>(&mut self, rag_ids: I)+ where+ I: IntoIterator<Item = S>,+ S: AsRef<str>,+ {+ // Ensure a filter object exists+ let filters = self+ .retriever+ .options+ .filters+ .get_or_insert_with(|| json!({}));++ let arr = filters+ .as_object_mut()+ .expect("filters must be a JSON object")+ .entry("rag_ids")+ .or_insert_with(|| json!([]))+ .as_array_mut()+ .expect("`rag_ids` must be an array");++ let mut existing: HashSet<String> = arr+ .iter()+ .filter_map(|v| v.as_str().map(|s| s.to_string()))+ .collect();++ // Insert new IDs if not already present+ for id in rag_ids {+ let id_str = id.as_ref();+ if existing.insert(id_str.to_string()) {+ arr.push(json!(id_str));+ }+ }+ }++ pub fn remove_rag_ids(&mut self, rag_ids: Vec<String>) {+ if let Some(filters) = self.retriever.options.filters.as_mut() {+ if let Some(current_rag_ids) = filters+ .get_mut("rag_ids")+ .and_then(|filter| filter.as_array_mut())+ {+ // Remove specified rag_ids from the array+ current_rag_ids.retain(|id| {+ id.as_str()+ .map_or(true, |id_str| !rag_ids.contains(&id_str.to_string()))+ });+ }+ }+ }++ async fn get_question(+ &self,+ history: &[Message],+ input: &str,+ ) -> Result<(String, Option<TokenUsage>), ChainError> {+ if history.is_empty() {+ return Ok((input.to_string(), None));+ }+ let mut token_usage: Option<TokenUsage> = None;+ let question = match self.rephrase_question {+ true => {+ let result = self+ .condense_question_chain+ .call(+ CondenseQuestionPromptBuilder::new()+ .question(input)+ .chat_history(history)+ .build(),+ )+ .await?;+ if let Some(tokens) = result.tokens {+ token_usage = Some(tokens);+ };+ result.generation+ },+ false => input.to_string(),+ };++ Ok((question, token_usage))+ }+}++#[async_trait]+impl Chain for ConversationalRetrieverChain {+ async fn call(&self, input_variables: PromptArgs) -> Result<GenerateResult, ChainError> {+ let output = self.execute(input_variables).await?;+ let result: GenerateResult = serde_json::from_value(output[DEFAULT_RESULT_KEY].clone())?;+ Ok(result)+ }++ async fn execute(+ &self,+ input_variables: PromptArgs,+ ) -> Result<HashMap<String, Value>, ChainError> {+ let mut token_usage: Option<TokenUsage> = None;+ let input_variable = &input_variables+ .get(&self.input_key)+ .ok_or(ChainError::MissingInputVariable(self.input_key.clone()))?;++ let human_message = Message::new_human_message(input_variable);+ let history = {+ let memory = self.memory.lock().await;+ memory.messages()+ };++ let (question, token) = self.get_question(&history, &human_message.content).await?;+ if let Some(token) = token {+ token_usage = Some(token);+ }++ let documents = self+ .retriever+ .get_relevant_documents(&question)+ .await+ .map_err(|e| ChainError::RetrieverError(e.to_string()))?;++ let mut output = self+ .combine_documents_chain+ .call(+ StuffQAPromptBuilder::new()+ .documents(&documents)+ .question(question.clone())+ .build(),+ )+ .await?;++ if let Some(tokens) = &output.tokens {+ if let Some(mut token_usage) = token_usage {+ token_usage.add(tokens);+ output.tokens = Some(token_usage)+ }+ }++ {+ let mut memory = self.memory.lock().await;+ memory.add_message(human_message);+ memory.add_message(Message::new_ai_message(&output.generation));+ }++ let mut result = HashMap::new();+ result.insert(self.output_key.clone(), json!(output.generation));++ result.insert(DEFAULT_RESULT_KEY.to_string(), json!(output));++ if self.return_source_documents {+ result.insert(+ CONVERSATIONAL_RETRIEVAL_QA_DEFAULT_SOURCE_DOCUMENT_KEY.to_string(),+ json!(documents),+ );+ }++ if self.rephrase_question {+ result.insert(+ CONVERSATIONAL_RETRIEVAL_QA_DEFAULT_GENERATED_QUESTION_KEY.to_string(),+ json!(question),+ );+ }++ Ok(result)+ }++ async fn stream(+ &self,+ input_variables: PromptArgs,+ ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamData, ChainError>> + Send>>, ChainError> {+ let input_variable = &input_variables+ .get(&self.input_key)+ .ok_or(ChainError::MissingInputVariable(self.input_key.clone()))?;++ let human_message = Message::new_human_message(input_variable);+ let history = {+ let memory = self.memory.lock().await;+ memory.messages()+ };++ let (question, _) = self.get_question(&history, &human_message.content).await?;++ let documents = self+ .retriever+ .get_relevant_documents(&question)+ .await+ .map_err(|e| ChainError::RetrieverError(e.to_string()))?;++ let stream = self+ .combine_documents_chain+ .stream(+ StuffQAPromptBuilder::new()+ .documents(&documents)+ .question(question.clone())+ .build(),+ )+ .await?;++ let sources = deduplicate_metadata(&documents);+ let memory = self.memory.clone();+ let complete_ai_message = Arc::new(Mutex::new(String::new()));+ let complete_ai_message_clone = complete_ai_message.clone();+ let output_stream = stream! {+ pin_mut!(stream);++ for source in sources {+ yield Ok(StreamData::new(+ json!({"source": source}),+ None,+ "".to_string(),+ ));+ }+++ while let Some(result) = stream.next().await {+ match result {+ Ok(data) => {+ let mut complete_ai_message_clone =+ complete_ai_message_clone.lock().await;+ complete_ai_message_clone.push_str(&data.content);++ yield Ok(data);+ },+ Err(e) => {+ yield Err(e);+ }+ }+ }++ let mut memory = memory.lock().await;+ memory.add_message(human_message);+ memory.add_message(Message::new_ai_message(&complete_ai_message.lock().await));+ };++ Ok(Box::pin(output_stream))+ }++ fn get_input_keys(&self) -> Vec<String> {+ vec![self.input_key.clone()]+ }++ fn get_output_keys(&self) -> Vec<String> {+ let mut keys = Vec::new();+ if self.return_source_documents {+ keys.push(CONVERSATIONAL_RETRIEVAL_QA_DEFAULT_SOURCE_DOCUMENT_KEY.to_string());+ }++ if self.rephrase_question {+ keys.push(CONVERSATIONAL_RETRIEVAL_QA_DEFAULT_GENERATED_QUESTION_KEY.to_string());+ }++ keys.push(self.output_key.clone());+ keys.push(DEFAULT_RESULT_KEY.to_string());++ keys+ }+}++pub struct ConversationalRetrieverChainBuilder {+ llm: Option<LLMOllama>,+ retriever: Option<AFRetriever<Value>>,+ memory: Option<Arc<Mutex<dyn BaseMemory>>>,+ combine_documents_chain: Option<Box<dyn Chain>>,+ condense_question_chain: Option<Box<dyn Chain>>,+ prompt: Option<Box<dyn FormatPrompter>>,+ rephrase_question: bool,+ return_source_documents: bool,+ input_key: String,+ output_key: String,+}+impl ConversationalRetrieverChainBuilder {+ pub fn new() -> Self {+ ConversationalRetrieverChainBuilder {+ llm: None,+ retriever: None,+ memory: None,+ combine_documents_chain: None,+ condense_question_chain: None,+ prompt: None,+ rephrase_question: true,+ return_source_documents: true,+ input_key: CONVERSATIONAL_RETRIEVAL_QA_DEFAULT_INPUT_KEY.to_string(),+ output_key: DEFAULT_OUTPUT_KEY.to_string(),+ }+ }++ pub fn retriever(mut self, retriever: AFRetriever<Value>) -> Self {+ self.retriever = Some(retriever);+ self+ }++ ///If you want to add a custom prompt,keep in mind which variables are obligatory.+ pub fn prompt<P: Into<Box<dyn FormatPrompter>>>(mut self, prompt: P) -> Self {+ self.prompt = Some(prompt.into());+ self+ }++ pub fn memory(mut self, memory: Arc<Mutex<dyn BaseMemory>>) -> Self {+ self.memory = Some(memory);+ self+ }++ pub fn llm(mut self, llm: LLMOllama) -> Self {+ self.llm = Some(llm);+ self+ }++ ///Chain designed to take the documents and the question and generate an output+ #[allow(dead_code)]+ pub fn combine_documents_chain<C: Into<Box<dyn Chain>>>(+ mut self,+ combine_documents_chain: C,+ ) -> Self {+ self.combine_documents_chain = Some(combine_documents_chain.into());+ self+ }++ ///Chain designed to reformulate the question based on the cat history+ #[allow(dead_code)]+ pub fn condense_question_chain<C: Into<Box<dyn Chain>>>(+ mut self,+ condense_question_chain: C,+ ) -> Self {+ self.condense_question_chain = Some(condense_question_chain.into());+ self+ }++ pub fn rephrase_question(mut self, rephrase_question: bool) -> Self {+ self.rephrase_question = rephrase_question;+ self+ }++ #[allow(dead_code)]+ pub fn return_source_documents(mut self, return_source_documents: bool) -> Self {+ self.return_source_documents = return_source_documents;+ self+ }++ pub fn build(mut self) -> Result<ConversationalRetrieverChain, ChainError> {+ if let Some(llm) = self.llm.as_ref() {+ let combine_documents_chain = {+ let mut builder = StuffDocumentBuilder::new().llm(llm.clone());+ if let Some(prompt) = self.prompt {+ builder = builder.prompt(prompt);+ }+ builder.build()?+ };+ let condense_question_chain = CondenseQuestionGeneratorChain::new(llm.clone());+ self.combine_documents_chain = Some(Box::new(combine_documents_chain));+ self.condense_question_chain = Some(Box::new(condense_question_chain));+ }++ let retriever = self+ .retriever+ .ok_or_else(|| ChainError::MissingObject("Retriever must be set".into()))?;++ let memory = self+ .memory+ .unwrap_or_else(|| Arc::new(Mutex::new(SimpleMemory::new())));++ let combine_documents_chain = self.combine_documents_chain.ok_or_else(|| {+ ChainError::MissingObject("Combine documents chain must be set or llm must be set".into())+ })?;+ let condense_question_chain = self.condense_question_chain.ok_or_else(|| {+ ChainError::MissingObject("Condense question chain must be set or llm must be set".into())+ })?;++ Ok(ConversationalRetrieverChain {+ ollama: self.llm.unwrap(),+ retriever,+ memory,+ combine_documents_chain,+ condense_question_chain,+ rephrase_question: self.rephrase_question,+ return_source_documents: self.return_source_documents,+ input_key: self.input_key,+ output_key: self.output_key,+ })+ }+}++// Retriever is a retriever for vector stores.+pub struct AFRetriever<F> {+ vstore: Box<dyn VectorStore<Options = VecStoreOptions<F>>>,+ num_docs: usize,+ options: VecStoreOptions<F>,+}+impl<F> AFRetriever<F> {+ pub fn new<V: Into<Box<dyn VectorStore<Options = VecStoreOptions<F>>>>>(+ vstore: V,+ num_docs: usize,+ ) -> Self {+ AFRetriever {+ vstore: vstore.into(),+ num_docs,+ options: VecStoreOptions::<F>::new(),+ }+ }++ pub fn with_options(mut self, options: VecStoreOptions<F>) -> Self {+ self.options = options;+ self+ }+}++#[async_trait]+impl<O: Sync + Send> Retriever for AFRetriever<O> {+ async fn get_relevant_documents(&self, query: &str) -> Result<Vec<Document>, Box<dyn Error>> {+ self+ .vstore+ .similarity_search(query, self.num_docs, &self.options)+ .await+ }+}++/// Deduplicates metadata from a list of documents by merging metadata entries with the same keys+fn deduplicate_metadata(documents: &[Document]) -> Vec<Value> {+ let mut merged_metadata: HashMap<String, Value> = HashMap::new();+ for document in documents {+ if let Some(object_id) = document.metadata.get(SOURCE_ID).and_then(|s| s.as_str()) {+ merged_metadata.insert(object_id.to_string(), json!(document.metadata.clone()));+ }+ }+ merged_metadata.into_values().collect()+}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/chat/llm.rs b/frontend/rust-lib/flowy-ai/src/local_ai/chat/llm.rsnew file mode 100644index 0000000000..1d482cd217--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/chat/llm.rs@@ -0,0 +1,119 @@+use async_trait::async_trait;+use futures::Stream;+use langchain_rust::language_models::llm::LLM;+use langchain_rust::language_models::{GenerateResult, LLMError, TokenUsage};+use langchain_rust::schemas::{Message, StreamData};+use ollama_rs::error::OllamaError;+use std::pin::Pin;+use std::sync::Arc;+use tokio_stream::StreamExt;++use ollama_rs::generation::chat::request::ChatMessageRequest;+use ollama_rs::generation::parameters::FormatType;+use ollama_rs::models::ModelOptions;+use ollama_rs::Ollama;++#[derive(Debug, Clone)]+pub struct LLMOllama {+ pub model_name: String,+ ollama: Arc<Ollama>,+ format: Option<FormatType>,+ options: Option<ModelOptions>,+}++impl Default for LLMOllama {+ fn default() -> Self {+ LLMOllama {+ model_name: "llama3.1".to_string(),+ ollama: Arc::new(Ollama::default()),+ format: None,+ options: None,+ }+ }+}++impl LLMOllama {+ pub fn new(+ model: &str,+ ollama: Arc<Ollama>,+ format: Option<FormatType>,+ options: Option<ModelOptions>,+ ) -> Self {+ LLMOllama {+ model_name: model.to_string(),+ ollama,+ format,+ options,+ }+ }++ pub fn with_options(mut self, options: ModelOptions) -> Self {+ self.options = Some(options);+ self+ }++ pub fn with_format(mut self, format: FormatType) -> Self {+ self.format = Some(format);+ self+ }++ pub fn with_model(mut self, model: &str) -> Self {+ self.model_name = model.to_string();+ self+ }++ pub fn set_model(&mut self, model: &str) {+ self.model_name = model.to_string();+ }++ fn generate_request(&self, messages: &[Message]) -> ChatMessageRequest {+ let mapped_messages = messages.iter().map(|message| message.into()).collect();+ let mut request = ChatMessageRequest::new(self.model_name.clone(), mapped_messages);+ if let Some(option) = &self.options {+ request = request.options(option.clone())+ }+ if let Some(format) = &self.format {+ request = request.format(format.clone());+ }+ request+ }+}++#[async_trait]+impl LLM for LLMOllama {+ async fn generate(&self, messages: &[Message]) -> Result<GenerateResult, LLMError> {+ let request = self.generate_request(messages);+ let result = self.ollama.send_chat_messages(request).await?;+ let generation = result.message.content;+ let tokens = result.final_data.map(|final_data| {+ let prompt_tokens = final_data.prompt_eval_count as u32;+ let completion_tokens = final_data.eval_count as u32;+ TokenUsage {+ prompt_tokens,+ completion_tokens,+ total_tokens: prompt_tokens + completion_tokens,+ }+ });++ Ok(GenerateResult { tokens, generation })+ }++ async fn stream(+ &self,+ messages: &[Message],+ ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamData, LLMError>> + Send>>, LLMError> {+ let request = self.generate_request(messages);+ let result = self.ollama.send_chat_messages_stream(request).await?;++ let stream = result.map(|data| match data {+ Ok(data) => Ok(StreamData::new(+ serde_json::to_value(&data).unwrap_or_default(),+ None,+ data.message.content,+ )),+ Err(_) => Err(OllamaError::Other("Stream error".to_string()).into()),+ });++ Ok(Box::pin(stream))+ }+}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/chat/llm_chat.rs b/frontend/rust-lib/flowy-ai/src/local_ai/chat/llm_chat.rsnew file mode 100644index 0000000000..6748d76c70--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/chat/llm_chat.rs@@ -0,0 +1,245 @@+use crate::local_ai::chat::conversation_chain::{+ AFRetriever, ConversationalRetrieverChain, ConversationalRetrieverChainBuilder,+};+use crate::local_ai::chat::llm::LLMOllama;+use crate::local_ai::chat::OllamaClientRef;+use crate::local_ai::prompt::format_prompt;+use crate::SqliteVectorStore;+use flowy_ai_pub::cloud::{QuestionStreamValue, ResponseFormat, StreamAnswer};+use flowy_ai_pub::entities::SOURCE_ID;+use flowy_error::{FlowyError, FlowyResult};+use futures::StreamExt;+use langchain_rust::chain::{Chain, ChainError};+use langchain_rust::memory::SimpleMemory;+use langchain_rust::prompt::{HumanMessagePromptTemplate, MessageFormatterStruct};+use langchain_rust::schemas::{Document, Message};+use langchain_rust::vectorstore::{VecStoreOptions, VectorStore};+use langchain_rust::{fmt_message, fmt_template, message_formatter, prompt_args, template_jinja2};+use serde_json::{json, Value};+use std::collections::HashMap;+use uuid::Uuid;++pub struct LLMChat {+ workspace_id: Uuid,+ #[allow(dead_code)]+ chat_id: Uuid,+ store: Option<SqliteVectorStore>,+ chain: ConversationalRetrieverChain,+ client: OllamaClientRef,+ current_format: ResponseFormat,+ rag_ids: Vec<String>,+}++impl LLMChat {+ pub async fn new(+ workspace_id: Uuid,+ chat_id: Uuid,+ model: &str,+ client: OllamaClientRef,+ store: Option<SqliteVectorStore>,+ rag_ids: Vec<String>,+ ) -> FlowyResult<Self> {+ let current_format = ResponseFormat::default();+ let chain = create_chain(+ &workspace_id,+ model,+ &client,+ rag_ids.clone(),+ ¤t_format,+ store.clone(),+ )+ .await?;++ Ok(Self {+ workspace_id,+ chat_id,+ store,+ chain,+ client,+ current_format,+ rag_ids,+ })+ }++ pub async fn set_chat_model(&mut self, model: &str) -> FlowyResult<()> {+ self.chain.ollama.set_model(model);+ Ok(())+ }++ pub async fn add_rag_id(&mut self, id: String) -> FlowyResult<()> {+ self.chain.add_rag_ids(vec![id]);+ Ok(())+ }++ pub async fn set_rag_ids(&mut self, rag_ids: Vec<String>) -> FlowyResult<()> {+ self.chain.set_rag_ids(rag_ids);+ Ok(())+ }++ pub fn remove_rag_id(&mut self, id: String) {+ self.chain.remove_rag_ids(vec![id]);+ }++ pub async fn embed_paragraphs(+ &self,+ object_id: &str,+ paragraphs: Vec<String>,+ ) -> FlowyResult<()> {+ let mut metadata = HashMap::new();+ metadata.insert("workspace_id".to_string(), json!(self.workspace_id));+ metadata.insert(SOURCE_ID.to_string(), json!(object_id));+ let document = Document::new(paragraphs.join("\n\n")).with_metadata(metadata);+ if let Some(store) = &self.store {+ store+ .add_documents(&[document], &VecStoreOptions::default())+ .await+ .map_err(|err| FlowyError::local_ai().with_context(err))?;+ }+ Ok(())+ }++ pub async fn ask_question(&self, question: &str) -> FlowyResult<String> {+ let input_variables = prompt_args! {+ "question" => question,+ };++ let result = self+ .chain+ .invoke(input_variables)+ .await+ .map_err(map_chain_error)?;+ Ok(result)+ }++ /// Send a message to the chat and get a response+ pub async fn stream_question(+ &mut self,+ message: &str,+ format: ResponseFormat,+ ) -> Result<StreamAnswer, FlowyError> {+ if self.current_format.output_layout != format.output_layout {+ self.current_format = format.clone();+ self.chain = create_chain(+ &self.workspace_id,+ self.chain.ollama.model_name.as_ref(),+ &self.client,+ self.rag_ids.clone(),+ &self.current_format,+ self.store.clone(),+ )+ .await?;+ }++ let input_variables = prompt_args! {+ "question" => message,+ };++ let stream_result = self.chain.stream(input_variables).await;+ let stream = stream_result.map_err(map_chain_error)?;+ let transformed_stream = stream.map(|result| {+ result+ .map(|stream_data| {+ if let Some(source) = stream_data.value.as_object().and_then(|v| v.get("source")) {+ QuestionStreamValue::Metadata {+ value: source.clone(),+ }+ } else {+ QuestionStreamValue::Answer {+ value: stream_data.content,+ }+ }+ })+ .map_err(map_chain_error)+ });+ Ok(Box::pin(transformed_stream))+ }+}++fn create_prompt_with_format(format: &ResponseFormat) -> MessageFormatterStruct {+ let format_instruction = format_prompt(format);+ message_formatter![+ fmt_message!(Message::new_system_message(+ "You are a helpful assistant",+ )),+ fmt_message!(format_instruction),+ fmt_template!(HumanMessagePromptTemplate::new(+ template_jinja2!("+ Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.+ {{context}}++ Question:{{question}}+ Answer:+ ",+ "context",+ "question"+ )+ ))+ ]+}++fn create_retriever(+ workspace_id: &Uuid,+ rag_ids: Vec<String>,+ store: SqliteVectorStore,+) -> AFRetriever<Value> {+ let options = VecStoreOptions::default()+ .with_score_threshold(0.4)+ .with_filters(json!({"rag_ids": rag_ids, "workspace_id": workspace_id}));+ AFRetriever::<Value>::new(store, 5).with_options(options)+}++async fn create_chain(+ workspace_id: &Uuid,+ model: &str,+ client: &OllamaClientRef,+ rag_ids: Vec<String>,+ format: &ResponseFormat,+ store: Option<SqliteVectorStore>,+) -> FlowyResult<ConversationalRetrieverChain> {+ let llm = create_llm(client, model).await?;+ let prompt = create_prompt_with_format(format);++ let mut builder = ConversationalRetrieverChainBuilder::new()+ .llm(llm)+ .rephrase_question(true)+ .memory(SimpleMemory::new().into());++ if let Some(store) = store {+ let retriever = create_retriever(workspace_id, rag_ids, store);+ builder = builder.retriever(retriever);+ }++ let chain = builder+ .prompt(prompt)+ .build()+ .map_err(|err| FlowyError::local_ai().with_context(err))?;++ Ok(chain)+}++async fn create_llm(client: &OllamaClientRef, model: &str) -> FlowyResult<LLMOllama> {+ let read_guard = client.read().await;+ let client = read_guard+ .as_ref()+ .ok_or_else(|| FlowyError::local_ai().with_context("Ollama client not initialized"))?+ .upgrade()+ .ok_or_else(|| FlowyError::local_ai().with_context("Ollama client has been dropped"))?+ .clone();+ let ollama = LLMOllama::new(model, client, None, None);+ Ok(ollama)+}++fn map_chain_error(err: ChainError) -> FlowyError {+ match err {+ ChainError::MissingInputVariable(var) => {+ FlowyError::local_ai().with_context(format!("Missing input variable: {}", var))+ },+ ChainError::MissingObject(obj) => {+ FlowyError::local_ai().with_context(format!("Missing object: {}", obj))+ },+ ChainError::RetrieverError(err) => {+ FlowyError::local_ai().with_context(format!("Retriever error: {}", err))+ },+ _ => FlowyError::local_ai().with_context(format!("Chain error: {:?}", err)),+ }+}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/chat/mod.rs b/frontend/rust-lib/flowy-ai/src/local_ai/chat/mod.rsnew file mode 100644index 0000000000..f89b7addbe--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/chat/mod.rs@@ -0,0 +1,219 @@+mod conversation_chain;+pub mod llm;+pub mod llm_chat;+pub mod related_question_chain;++use crate::local_ai::chat::llm::LLMOllama;+use crate::local_ai::chat::llm_chat::LLMChat;+use crate::local_ai::chat::related_question_chain::RelatedQuestionChain;+use crate::local_ai::completion::chain::CompletionChain;+use crate::local_ai::database::summary::DatabaseSummaryChain;+use crate::local_ai::database::translate::DatabaseTranslateChain;+use crate::SqliteVectorStore;+use dashmap::DashMap;+use flowy_ai_pub::cloud::ai_dto::{TranslateRowData, TranslateRowResponse};+use flowy_ai_pub::cloud::chat_dto::ChatAuthorType;+use flowy_ai_pub::cloud::{+ CompleteTextParams, CompletionType, ResponseFormat, StreamAnswer, StreamComplete,+};+use flowy_ai_pub::persistence::select_latest_user_message;+use flowy_ai_pub::user_service::AIUserService;+use flowy_database_pub::cloud::{SummaryRowContent, TranslateRowContent};+use flowy_error::{FlowyError, FlowyResult};+use futures_util::StreamExt;+use ollama_rs::Ollama;+use serde_json::Value;+use std::collections::HashMap;+use std::path::PathBuf;+use std::sync::{Arc, Weak};+use tokio::sync::RwLock;+use tracing::trace;+use uuid::Uuid;++type OllamaClientRef = Arc<RwLock<Option<Weak<Ollama>>>>;++pub struct LLMChatController {+ chat_by_id: DashMap<Uuid, LLMChat>,+ store: RwLock<Option<SqliteVectorStore>>,+ client: OllamaClientRef,+ user_service: Weak<dyn AIUserService>,+}+impl LLMChatController {+ pub fn new(user_service: Weak<dyn AIUserService>) -> Self {+ Self {+ store: RwLock::new(None),+ chat_by_id: DashMap::new(),+ client: Default::default(),+ user_service,+ }+ }++ pub async fn is_ready(&self) -> bool {+ self.client.read().await.is_some()+ }++ #[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]+ pub async fn initialize(+ &self,+ ollama: Weak<Ollama>,+ vector_db: Weak<flowy_sqlite_vec::db::VectorSqliteDB>,+ ) {+ let store = SqliteVectorStore::new(ollama.clone(), vector_db);+ *self.client.write().await = Some(ollama);+ *self.store.write().await = Some(store);+ }++ pub async fn open_chat(+ &self,+ workspace_id: &Uuid,+ chat_id: &Uuid,+ model: &str,+ ) -> FlowyResult<()> {+ let store = self.store.read().await.clone();+ let chat = LLMChat::new(+ *workspace_id,+ *chat_id,+ model,+ self.client.clone(),+ store,+ vec![],+ )+ .await?;+ self.chat_by_id.insert(*chat_id, chat);+ Ok(())+ }++ pub fn close_chat(&self, chat_id: &Uuid) {+ self.chat_by_id.remove(chat_id);+ }++ pub async fn summarize_database_row(+ &self,+ model_name: &str,+ data: SummaryRowContent,+ ) -> FlowyResult<String> {+ let client = self+ .client+ .read()+ .await+ .clone()+ .ok_or(FlowyError::local_ai())?+ .upgrade()+ .ok_or(FlowyError::local_ai())?;++ let chain = DatabaseSummaryChain::new(LLMOllama::new(model_name, client, None, None));+ let response = chain.summarize(data).await?;+ Ok(response)+ }++ pub async fn translate_database_row(+ &self,+ model_name: &str,+ cells: TranslateRowContent,+ language: &str,+ ) -> FlowyResult<TranslateRowResponse> {+ let client = self+ .client+ .read()+ .await+ .clone()+ .ok_or(FlowyError::local_ai())?+ .upgrade()+ .ok_or(FlowyError::local_ai())?;++ let chain = DatabaseTranslateChain::new(LLMOllama::new(model_name, client, None, None));+ let data = TranslateRowData {+ cells,+ language: language.to_string(),+ include_header: false,+ };+ let resp = chain.translate(data).await?;+ Ok(resp)+ }++ pub async fn complete_text(+ &self,+ model_name: &str,+ params: CompleteTextParams,+ ) -> Result<StreamComplete, FlowyError> {+ let client = self+ .client+ .read()+ .await+ .clone()+ .ok_or(FlowyError::local_ai())?+ .upgrade()+ .ok_or(FlowyError::local_ai())?;++ let chain = CompletionChain::new(LLMOllama::new(model_name, client, None, None));+ let ty = params.completion_type.unwrap_or(CompletionType::AskAI);+ let stream = chain+ .complete(¶ms.text, ty, params.format, params.metadata)+ .await?+ .boxed();+ Ok(stream)+ }++ pub async fn embed_file(+ &self,+ _chat_id: &Uuid,+ _file_path: PathBuf,+ _metadata: Option<HashMap<String, Value>>,+ ) -> FlowyResult<()> {+ Ok(())+ }++ pub async fn get_related_question(+ &self,+ model_name: &str,+ chat_id: &Uuid,+ _message_id: i64,+ ) -> FlowyResult<Vec<String>> {+ let client = self+ .client+ .read()+ .await+ .clone()+ .ok_or(FlowyError::local_ai())?+ .upgrade()+ .ok_or(FlowyError::local_ai())?;++ let user_service = self.user_service.upgrade().ok_or(FlowyError::local_ai())?;+ let uid = user_service.user_id()?;+ let conn = user_service.sqlite_connection(uid)?;+ let message = select_latest_user_message(conn, &chat_id.to_string(), ChatAuthorType::Human)?;++ let chain = RelatedQuestionChain::new(LLMOllama::new(model_name, client, None, None));+ let questions = chain.related_question(&message.content).await?;+ trace!(+ "related questions: {:?} for message: {}",+ questions,+ message.content+ );+ Ok(questions)+ }++ pub async fn ask_question(&self, chat_id: &Uuid, question: &str) -> FlowyResult<String> {+ if let Some(chat) = self.chat_by_id.get(chat_id) {+ let chat = chat.value();+ let response = chat.ask_question(question).await;+ return response;+ }++ Err(FlowyError::local_ai().with_context(format!("Chat with id {} not found", chat_id)))+ }++ pub async fn stream_question(+ &self,+ chat_id: &Uuid,+ question: &str,+ format: ResponseFormat,+ ) -> FlowyResult<StreamAnswer> {+ if let Some(mut chat) = self.chat_by_id.get_mut(chat_id) {+ let response = chat.stream_question(question, format).await;+ return response;+ }++ Err(FlowyError::local_ai().with_context(format!("Chat with id {} not found", chat_id)))+ }+}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/chat/related_question_chain.rs b/frontend/rust-lib/flowy-ai/src/local_ai/chat/related_question_chain.rsnew file mode 100644index 0000000000..73a48b0cb5--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/chat/related_question_chain.rs@@ -0,0 +1,52 @@+use crate::local_ai::chat::llm::LLMOllama;+use flowy_error::{FlowyError, FlowyResult};+use langchain_rust::language_models::llm::LLM;+use langchain_rust::schemas::Message;+use ollama_rs::generation::parameters::{FormatType, JsonStructure};+use schemars::JsonSchema;+use serde::Deserialize;++const SUMMARIZE_SYSTEM_PROMPT: &str = r#"+As an AppFlowy AI assistant, your task is to generate three medium-length, relevant, and informative questions based on the provided conversation history.+The output should only return a JSON instance that conforms to the JSON schema below.++{+ "questions": [+ "What are the key skills needed to tackle a black diamond slope in snowboarding?",+ "How does the difficulty of black diamond trails compare across different ski resorts?",+ "Can you provide tips for snowboarders preparing to try a black diamond trail for the first time?"+ ]+}+"#;++#[derive(Debug, Deserialize, JsonSchema)]+struct QuestionsResponse {+ questions: Vec<String>,+}++pub struct RelatedQuestionChain {+ llm: LLMOllama,+}++impl RelatedQuestionChain {+ pub fn new(ollama: LLMOllama) -> Self {+ let format = FormatType::StructuredJson(JsonStructure::new::<QuestionsResponse>());+ Self {+ llm: ollama.with_format(format),+ }+ }++ pub async fn related_question(&self, question: &str) -> FlowyResult<Vec<String>> {+ let messages = vec![+ Message::new_system_message(SUMMARIZE_SYSTEM_PROMPT),+ Message::new_human_message(question),+ ];++ let result = self.llm.generate(&messages).await.map_err(|err| {+ FlowyError::internal().with_context(format!("Error generating related questions: {}", err))+ })?;++ let parsed_result = serde_json::from_str::<QuestionsResponse>(&result.generation)?;+ Ok(parsed_result.questions)+ }+}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/completion/chain.rs b/frontend/rust-lib/flowy-ai/src/local_ai/completion/chain.rsnew file mode 100644index 0000000000..25a514d55e--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/completion/chain.rs@@ -0,0 +1,143 @@+use crate::local_ai::chat::llm::LLMOllama;+use crate::local_ai::completion::stream_interpreter::stream_interpreter_for_completion;+use crate::local_ai::completion::writer::{+ AskAiWriter, CompletionWriterContext, ContinueWriteWriter, CustomWriter, ExplainWriter,+ ImproveWritingWriter, MakeLongerWriter, SpellingGrammarWriter, SummaryWriter,+};+use crate::local_ai::prompt::{format_prompt, history_prompt};+use flowy_ai_pub::cloud::{+ CompletionMetadata, CompletionStreamValue, CompletionType, CustomPrompt, ResponseFormat,+};+use flowy_error::FlowyError;+use futures_util::StreamExt;+use langchain_rust::language_models::llm::LLM;+use langchain_rust::schemas::Message;+use std::collections::HashMap;+use tokio::sync::mpsc;+use tokio_stream::wrappers::ReceiverStream;+use tracing::error;++pub trait CompletionWriter: Send + Sync {+ fn system_message(&self) -> String;+ fn reformat_human_input(&self, content: &str) -> String {+ content.to_string()+ }+ fn reasoning_system_message(&self) -> String {+ self.system_message()+ }+ fn support_format(&self) -> bool;+ fn support_reasoning(&self) -> bool {+ false+ }+}++#[derive(Default)]+pub struct WriterConfig {+ pub custom_reasoning_system_prompt: HashMap<CompletionType, String>,+}++pub struct CompletionChain {+ ollama: LLMOllama,+}++impl CompletionChain {+ pub fn new(ollama: LLMOllama) -> Self {+ Self { ollama }+ }++ pub async fn complete(+ &self,+ text: &str,+ ty: CompletionType,+ format: ResponseFormat,+ mut metadata: Option<CompletionMetadata>,+ ) -> Result<ReceiverStream<anyhow::Result<CompletionStreamValue, FlowyError>>, FlowyError> {+ let custom_prompt = metadata.as_ref().and_then(|m| m.custom_prompt.clone());+ let writer = writer_from_type(&ty, &WriterConfig::default(), custom_prompt);++ // Build messages+ let mut messages = vec![];+ let system_msg = if writer.support_reasoning() {+ writer.reasoning_system_message()+ } else {+ writer.system_message()+ };+ messages.push(Message::new_system_message(system_msg));++ messages.push(+ if writer.support_format() {+ format_prompt(&format)+ } else {+ Message::new_system_message(+ "Respond naturally without formal phrases like 'Certainly', 'Absolutely', or 'Sure'. Keep the tone conversational, clear, and professional"+ )+ }+ );++ if let Some(history) = metadata.as_mut().and_then(|m| m.completion_history.take()) {+ messages.extend(history_prompt(Some(history)));+ }+ messages.push(Message::new_human_message(text));+ let raw_stream = self+ .ollama+ .stream(&messages)+ .await+ .map_err(|e| FlowyError::local_ai().with_context(e))?;++ let stream = stream_interpreter_for_completion(raw_stream, ty)+ .map(|res| res.map_err(|e| FlowyError::local_ai().with_context(e)))+ .boxed();++ // Spawn the forwarding task+ let (tx, rx) = mpsc::channel(32);+ spawn_stream(stream, tx);+ Ok(ReceiverStream::new(rx))+ }+}++fn spawn_stream<S>(+ mut stream: S,+ tx: mpsc::Sender<anyhow::Result<CompletionStreamValue, FlowyError>>,+) where+ S: tokio_stream::Stream<Item = anyhow::Result<CompletionStreamValue, FlowyError>>+ + Unpin+ + Send+ + 'static,+{+ tokio::spawn(async move {+ while let Some(item) = stream.next().await {+ if tx.send(item).await.is_err() {+ error!("[AICompletion] channel closed");+ break;+ }+ }+ });+}++/// Construct the appropriate writer based on type+pub fn writer_from_type(+ complete_type: &CompletionType,+ writer_config: &WriterConfig,+ custom_prompt: Option<CustomPrompt>,+) -> Box<dyn CompletionWriter> {+ let context = CompletionWriterContext::new(+ writer_config+ .custom_reasoning_system_prompt+ .get(complete_type)+ .cloned(),+ );++ match complete_type {+ CompletionType::CustomPrompt => match custom_prompt {+ Some(p) => Box::new(CustomWriter::new(p, context)),+ None => Box::new(AskAiWriter::new(context)),+ },+ CompletionType::ImproveWriting => Box::new(ImproveWritingWriter::new(context)),+ CompletionType::SpellingAndGrammar => Box::new(SpellingGrammarWriter::new(context)),+ CompletionType::MakeShorter => Box::new(SummaryWriter::new(context)),+ CompletionType::MakeLonger => Box::new(MakeLongerWriter::new(context)),+ CompletionType::ContinueWriting => Box::new(ContinueWriteWriter::new(context)),+ CompletionType::Explain => Box::new(ExplainWriter::new(context)),+ CompletionType::AskAI => Box::new(AskAiWriter::new(context)),+ }+}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/completion/impls.rs b/frontend/rust-lib/flowy-ai/src/local_ai/completion/impls.rsnew file mode 100644index 0000000000..8b13789179--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/completion/impls.rs@@ -0,0 +1 @@+diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/completion/mod.rs b/frontend/rust-lib/flowy-ai/src/local_ai/completion/mod.rsnew file mode 100644index 0000000000..b04b7350d2--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/completion/mod.rs@@ -0,0 +1,4 @@+pub mod chain;+pub mod impls;+pub mod stream_interpreter;+pub mod writer;diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/completion/stream_interpreter.rs b/frontend/rust-lib/flowy-ai/src/local_ai/completion/stream_interpreter.rsnew file mode 100644index 0000000000..0cf4500c33--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/completion/stream_interpreter.rs@@ -0,0 +1,520 @@+use flowy_ai_pub::cloud::{CompletionStreamValue, CompletionType};+use futures_util::{stream, stream::BoxStream, Stream, StreamExt};+use langchain_rust::language_models::LLMError;+use langchain_rust::schemas::StreamData;+use serde_json::{json, Value};+use std::collections::HashMap;+use std::sync::{Arc, Mutex};+use tracing::{debug, warn};++pub fn stream_interpreter_for_completion(+ input_stream: BoxStream<'static, Result<StreamData, LLMError>>,+ completion_type: CompletionType,+) -> BoxStream<'static, Result<CompletionStreamValue, LLMError>> {+ match completion_type {+ CompletionType::ImproveWriting | CompletionType::SpellingAndGrammar => {+ interpret_completion_stream(input_stream, completion_type)+ },+ _ => input_stream+ .map(|res| res.map(map_response_to_completion_value))+ .boxed(),+ }+}++fn map_response_to_completion_value(data: StreamData) -> CompletionStreamValue {+ if let Some(content) = data.value.get("content") {+ return CompletionStreamValue::Answer {+ value: content.as_str().unwrap_or_default().to_string(),+ };+ }++ if let Some(content) = data.value.get("comment") {+ return CompletionStreamValue::Comment {+ value: content.as_str().unwrap_or_default().to_string(),+ };+ }++ warn!("[AICompletion] unexpected stream data: {:?}", data);+ CompletionStreamValue::Answer {+ value: data.content,+ }+}++/// Creates a mapping for the ImproveWriting completion type+fn create_improve_writing_mapping() -> HashMap<String, String> {+ let mut mapping = HashMap::new();+ // Support multiple variants of the tag names+ mapping.insert("Improved".to_string(), "content".to_string());+ mapping.insert("improved".to_string(), "content".to_string());+ mapping.insert("IMPROVED".to_string(), "content".to_string());+ mapping.insert("Explanation".to_string(), "comment".to_string());+ mapping.insert("explanation".to_string(), "comment".to_string());+ mapping+}++/// Creates a mapping for the SpellingAndGrammar completion type+fn create_spelling_grammar_mapping() -> HashMap<String, String> {+ let mut mapping = HashMap::new();+ // Support multiple variants of the tag names+ mapping.insert("Corrected".to_string(), "content".to_string());+ mapping.insert("corrected".to_string(), "content".to_string());+ mapping.insert("Correct".to_string(), "content".to_string());+ mapping.insert("correct".to_string(), "content".to_string());+ mapping.insert("CORRECTED".to_string(), "content".to_string());+ mapping.insert("Corrected:".to_string(), "content".to_string());+ mapping.insert("Explanation".to_string(), "comment".to_string());+ mapping.insert("explanation".to_string(), "comment".to_string());+ mapping+}++/// Process a stream of completion data with the interpreter+///+/// This function takes an input stream and a completion type, and returns a new stream+/// where each item has been processed by the StreamInterpreter.+///+/// The interpreter extracts content between specific XML-like tags based on the mapping+/// for the given completion type.+pub fn interpret_completion_stream<S>(+ stream: S,+ completion_type: CompletionType,+) -> BoxStream<'static, Result<CompletionStreamValue, LLMError>>+where+ S: Stream<Item = Result<StreamData, LLMError>> + Send + 'static,+{+ let mapping = match completion_type {+ CompletionType::ImproveWriting => create_improve_writing_mapping(),+ CompletionType::SpellingAndGrammar => create_spelling_grammar_mapping(),+ _ => HashMap::new(),+ };++ if mapping.is_empty() {+ // If no mapping exists for this completion type, return the original stream+ return stream+ .map(|res| res.map(map_response_to_completion_value))+ .boxed();+ }++ // Create a shared interpreter that will be used across all stream items+ let interpreter = Arc::new(Mutex::new(StreamInterpreter::new()));+ let section_mapping = Arc::new(mapping);++ // First, process all stream items+ let interpreter_clone = interpreter.clone();+ let section_mapping_clone = section_mapping.clone();+ let processed_stream = stream.filter_map(move |result| {+ let interpreter = interpreter_clone.clone();+ let mapping = section_mapping_clone.clone();+ async move {+ match result {+ Ok(data) => {+ // Lock the interpreter only when needed+ let mut interpreter_guard = interpreter.lock().unwrap();+ interpreter_guard+ .post_process_output(&data.value, &mapping)+ .map(completion_stream_value_from_value)+ },+ Err(err) => Some(Err(err)),+ }+ }+ });++ // Then, handle any remaining data after the stream is done+ let final_stream = stream::once(async move {+ let mut interpreter_guard = interpreter.lock().unwrap();+ interpreter_guard+ .consume_post_process_pending(§ion_mapping)+ .map(completion_stream_value_from_value)+ })+ .filter_map(|x| async { x });++ // Combine the processed stream with the final stream+ Box::pin(processed_stream.chain(final_stream))+}++fn completion_stream_value_from_value(value: Value) -> Result<CompletionStreamValue, LLMError> {+ // trace!("[AICompletion] processing stream value: {:?}", value);++ if let Some(content) = value.get("content") {+ return Ok(CompletionStreamValue::Answer {+ value: content.as_str().unwrap_or_default().to_string(),+ });+ }++ if let Some(comment) = value.get("comment") {+ return Ok(CompletionStreamValue::Comment {+ value: comment.as_str().unwrap_or_default().to_string(),+ });+ }++ Err(LLMError::ContentNotFound(format!(+ "Unexpected stream data: {:?}",+ value+ )))+}++struct StreamInterpreter {+ section_buffer: String,+ section: Option<String>,+ section_is_started: HashMap<String, bool>,+ start_tag: Option<String>,+ end_tag: Option<String>,+ end_tag_len: usize,+ check_tag_len: usize,+ // Track tag style for matching appropriate end tags+ tag_style: Option<TagStyle>,+}++// Define supported tag styles+#[derive(Debug, Clone, PartialEq)]+enum TagStyle {+ Xml, // <Tag>...</Tag>+ Markdown, // **Tag**...**Tag**+ #[allow(dead_code)]+ MixedFormat, // **Tag**...</Tag> or other mixed styles+}++impl StreamInterpreter {+ /// Creates a new TagProcessingMixin.+ pub fn new() -> Self {+ StreamInterpreter {+ section_buffer: String::new(),+ section: None,+ section_is_started: Default::default(),+ start_tag: None,+ end_tag: None,+ end_tag_len: 0,+ check_tag_len: 0,+ tag_style: None,+ }+ }++ /// Creates a Value object from content if it's not empty and the section name exists in the mapping+ fn create_value_from_content(+ &mut self,+ mut content: String,+ section_name: &str,+ section_mapping: &HashMap<String, String>,+ ) -> Option<Value> {+ if self+ .section_is_started+ .get(section_name)+ .cloned()+ .unwrap_or(true)+ {+ content = content.trim_start_matches('\n').to_string();+ self+ .section_is_started+ .insert(section_name.to_string(), true);+ }++ if content.is_empty() {+ return None;+ }+ let output_key = section_mapping.get(section_name)?;+ // trace!(+ // "[AICompletion] create value from content: {}, key: {}",+ // content,+ // output_key+ // );+ Some(json!({output_key: content}))+ }++ /// Finds the earliest start tag in the buffer+ ///+ /// Returns a tuple containing the position, name, tag, style, and length of the earliest tag,+ /// or None if no tags are found.+ fn find_earliest_start_tag(+ &self,+ buffer: &str,+ section_mapping: &HashMap<String, String>,+ ) -> Option<(usize, String, String, TagStyle, usize)> {+ let mut starts: Vec<(usize, String, String, TagStyle, usize)> = Vec::new();+ for section_name in section_mapping.keys() {+ if let Some((idx, tag, style, tag_len)) = self.detect_tag(buffer, section_name) {+ starts.push((idx, section_name.clone(), tag, style, tag_len));+ }+ }++ if starts.is_empty() {+ return None;+ }++ // Pick the earliest start tag+ starts.sort_by_key(|&(idx, _, _, _, _)| idx);+ Some(starts[0].clone())+ }++ /// Detects a tag in multiple formats+ fn detect_tag(&self, text: &str, tag_name: &str) -> Option<(usize, String, TagStyle, usize)> {+ // Check XML style: <Tag>+ let xml_tag = format!("<{}>", tag_name);+ if let Some(pos) = text.find(&xml_tag) {+ let xml_tag_len = xml_tag.len();+ return Some((pos, xml_tag, TagStyle::Xml, xml_tag_len));+ }++ // Check Markdown style: **Tag**+ let md_tag = format!("**{}**", tag_name);+ if let Some(pos) = text.find(&md_tag) {+ let md_tag_len = md_tag.len();+ return Some((pos, md_tag, TagStyle::Markdown, md_tag_len));+ }++ // Check case-insensitive XML style+ let lowercase_text = text.to_lowercase();+ let lowercase_xml_tag = format!("<{}>", tag_name.to_lowercase());+ if let Some(pos) = lowercase_text.find(&lowercase_xml_tag) {+ let xml_tag_len = lowercase_xml_tag.len();+ return Some((pos, xml_tag, TagStyle::Xml, xml_tag_len));+ }++ // Check case-insensitive Markdown style+ let lowercase_md_tag = format!("**{}**", tag_name.to_lowercase());+ if let Some(pos) = lowercase_text.find(&lowercase_md_tag) {+ let md_tag_len = lowercase_md_tag.len();+ // Use the original case for the returned tag+ let original_md_tag = format!("**{}**", tag_name);+ return Some((pos, original_md_tag, TagStyle::Markdown, md_tag_len));+ }++ None+ }++ /// Generates appropriate end tag based on tag style and name+ fn generate_end_tag(&self, tag_name: &str, style: &TagStyle) -> (String, usize) {+ match style {+ TagStyle::Xml => {+ let tag = format!("</{}>", tag_name);+ (tag.clone(), tag.len())+ },+ TagStyle::Markdown => {+ let tag = format!("**{}**", tag_name);+ (tag.to_string(), tag.len())+ },+ TagStyle::MixedFormat => {+ // For mixed format, check both closing styles+ let xml_tag = format!("</{}>", tag_name);+ (xml_tag.clone(), xml_tag.len())+ },+ }+ }++ /// Find end tag in text considering multiple formats+ fn find_end_tag(&self, text: &str, tag_name: &str, style: &TagStyle) -> Option<(usize, usize)> {+ match style {+ TagStyle::Xml => {+ let end_tag = format!("</{}>", tag_name);+ if let Some(pos) = text.find(&end_tag) {+ return Some((pos, end_tag.len()));+ }++ // Try case-insensitive match as fallback+ let lowercase_text = text.to_lowercase();+ let lowercase_end_tag = format!("</{}>", tag_name.to_lowercase());+ if let Some(pos) = lowercase_text.find(&lowercase_end_tag) {+ return Some((pos, end_tag.len()));+ }+ },+ TagStyle::Markdown => {+ let end_tag = format!("**{}**", tag_name);+ if let Some(pos) = text.find(&end_tag) {+ return Some((pos, end_tag.len()));+ }+ },+ TagStyle::MixedFormat => {+ // Check both styles for mixed format+ let xml_end = format!("</{}>", tag_name);+ let md_end = format!("**{}**", tag_name);++ let xml_pos = text.find(&xml_end).map(|p| (p, xml_end.len()));+ let md_pos = text.find(&md_end).map(|p| (p, md_end.len()));++ // Return the first one found+ if let Some(xml_match) = xml_pos {+ if let Some(md_match) = md_pos {+ if xml_match.0 < md_match.0 {+ return Some(xml_match);+ } else {+ return Some(md_match);+ }+ } else {+ return Some(xml_match);+ }+ } else {+ return md_pos;+ }+ },+ }++ None+ }++ /// Processes an input JSON object, extracting content between tags based on a mapping.+ ///+ /// - `input`: JSON object with a key "content" containing the text chunk.+ /// - `section_mapping`: maps section names to output keys.+ ///+ /// Returns an optional JSON object containing the extracted section content.+ pub fn post_process_output(+ &mut self,+ input: &Value,+ section_mapping: &HashMap<String, String>,+ ) -> Option<Value> {+ // Only process JSON objects+ let obj = input.as_object()?;++ // Extract the new text chunk+ let new_text = obj+ .get("message")+ .and_then(|msg| msg.as_object())+ .and_then(|msg_obj| msg_obj.get("content"))+ .and_then(Value::as_str)?;++ // Append to the buffer+ debug!("new_text: {}", new_text);+ self.section_buffer.push_str(new_text);++ // If not currently inside a section, look for a start tag+ if self.section.is_none() {+ let earliest_tag = self.find_earliest_start_tag(&self.section_buffer, section_mapping);+ let (start_pos, selected_name, selected_tag, selected_style, tag_len) = earliest_tag?;++ self.section = Some(selected_name.clone());+ self.start_tag = Some(selected_tag.clone());+ self.tag_style = Some(selected_style.clone());++ let (end_tag, end_tag_len) = self.generate_end_tag(&selected_name, &selected_style);+ self.end_tag = Some(end_tag);+ self.end_tag_len = end_tag_len;+ self.check_tag_len = self.end_tag_len * 2;++ let start = start_pos + tag_len;+ if start < self.section_buffer.len() {+ let after_tag = &self.section_buffer[start..];+ if after_tag.starts_with('\n') && start + 1 < self.section_buffer.len() {+ self.section_buffer = self.section_buffer[(start + 1)..].to_string();+ } else {+ self.section_buffer = self.section_buffer[start..].to_string();+ }+ } else {+ self.section_buffer = String::new();+ }+ return None;+ }++ // We're inside a section: look for the end tag+ let (section_name, tag_style) = match (self.section.clone(), &self.tag_style) {+ (Some(name), Some(style)) => (name, style),+ _ => return None,+ };++ // If the buffer is too short, return None+ if self.section_buffer.len() < self.check_tag_len {+ return None;+ }++ match self.find_end_tag(&self.section_buffer, §ion_name, tag_style) {+ None => {+ let earliest_tag = self.find_earliest_start_tag(&self.section_buffer, section_mapping);+ match earliest_tag {+ Some((start_pos, selected_name, selected_tag, selected_style, tag_len)) => {+ if selected_name != section_name {+ // If it's a new section, process the current one+ let content = self.section_buffer[..start_pos].to_string();+ self.section_buffer = self.section_buffer[start_pos + tag_len..].to_string();+ self.section = Some(selected_name.clone());+ self.tag_style = Some(selected_style.clone());+ self.start_tag = Some(selected_tag.clone());+ let (end_tag, end_tag_len) = self.generate_end_tag(&selected_name, &selected_style);+ self.end_tag = Some(end_tag);+ self.end_tag_len = end_tag_len;++ let content = content.trim_end_matches('\n');+ self.create_value_from_content(content.to_string(), §ion_name, section_mapping)+ } else {+ let content = std::mem::take(&mut self.section_buffer);+ self.create_value_from_content(content, §ion_name, section_mapping)+ }+ },+ None => {+ // do not take all the section buffer, because the current buffer might contain+ // a uncompleted tag+ let content = self.section_buffer[..section_name.len()].to_string();+ self.section_buffer = self.section_buffer[section_name.len()..].to_string();+ self.create_value_from_content(content, §ion_name, section_mapping)+ },+ }+ },+ Some((end_pos, end_len)) => {+ let content = self.section_buffer[..end_pos].to_string();+ self.section_buffer = self.section_buffer[end_pos + end_len..].to_string();+ self.section = None;+ self.start_tag = None;++ let content = content.trim_end_matches('\n');+ self.create_value_from_content(content.to_string(), §ion_name, section_mapping)+ },+ }+ }++ /// Consume any remaining pending content after processing is complete.+ pub fn consume_post_process_pending(+ &mut self,+ section_mapping: &HashMap<String, String>,+ ) -> Option<Value> {+ if !self.section_buffer.is_empty() {+ let earliest_tag = self.find_earliest_start_tag(&self.section_buffer, section_mapping);+ if let Some((start_pos, selected_name, _, selected_style, tag_len)) = earliest_tag {+ // Set up the new section+ self.section = Some(selected_name.clone());+ self.tag_style = Some(selected_style.clone());++ // Skip past the start tag+ let start = start_pos + tag_len;+ if start < self.section_buffer.len() {+ self.section_buffer = self.section_buffer[start..].to_string();+ return match self.find_end_tag(&self.section_buffer, &selected_name, &selected_style) {+ None => {+ let trimmed_content = self.section_buffer.to_string();+ self.create_value_from_content(trimmed_content, &selected_name, section_mapping)+ },+ Some((end_pos, end_len)) => {+ let content = self.section_buffer[..end_pos].to_string();++ self.section_buffer = self.section_buffer[end_pos + end_len..].to_string();+ self.section = None;+ self.start_tag = None;++ self.create_value_from_content(+ content.trim_end_matches("\n").to_string(),+ &selected_name,+ section_mapping,+ )+ },+ };+ }+ }+ }++ match self.find_end_tag(+ &self.section_buffer,+ self.section.as_ref()?,+ self.tag_style.as_ref()?,+ ) {+ None => {+ let content = std::mem::take(&mut self.section_buffer)+ .trim_end_matches('\n')+ .to_string();+ let section_name = self.section.clone()?;+ self.create_value_from_content(content, §ion_name, section_mapping)+ },+ Some((end_pos, _)) => {+ let content = self.section_buffer[..end_pos]+ .trim_end_matches('\n')+ .to_string();+ let section_name = self.section.clone()?;+ self.create_value_from_content(content, §ion_name, section_mapping)+ },+ }+ }+}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/completion/writer.rs b/frontend/rust-lib/flowy-ai/src/local_ai/completion/writer.rsnew file mode 100644index 0000000000..96b3d42bc2--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/completion/writer.rs@@ -0,0 +1,280 @@+use crate::local_ai::completion::chain::CompletionWriter;+use flowy_ai_pub::cloud::CustomPrompt;++pub struct CompletionWriterContext {+ custom_system_reasoning_prompt: Option<String>,+}++impl CompletionWriterContext {+ pub fn new(custom_system_reasoning_prompt: Option<String>) -> Self {+ CompletionWriterContext {+ custom_system_reasoning_prompt,+ }+ }+}++// ImproveWritingWriter implementation+pub struct ImproveWritingWriter {+ context: CompletionWriterContext,+}++impl ImproveWritingWriter {+ pub fn new(context: CompletionWriterContext) -> Self {+ ImproveWritingWriter { context }+ }+}++impl CompletionWriter for ImproveWritingWriter {+ fn system_message(&self) -> String {+ r#"You are an expert in refining written material, making it engaging, clear, and effective.+Ensure that your final output maintains the same format and language as the human input."#+ .to_string()+ }++ fn reasoning_system_message(&self) -> String {+ if let Some(prompt) = &self.context.custom_system_reasoning_prompt {+ return prompt.clone();+ }++ r#"You are an expert writing coach specializing in making text more engaging, clear, and impactful+while preserving the original meaning and intent. Your task is to thoughtfully enhance any text I submit.++1. Provide an improved version of the text+2. Explain what was enhanced:+ - Identify specific areas of improvement (clarity, conciseness, word choice, etc.)+ - Use markdown formatting (**bold** or `code`) to highlight modified sections+ - If the text already appears optimal, acknowledge its strengths and return it unchanged+3.Maintain the original tone, style, and level of formality+4. Preserve the author's unique voice and perspective+5. Keep specialized terminology and technical language intact+6. Focus on enhancing readability and impact without changing the core message++Format your response like this:+<Improved>[The enhanced version of the text]</Improved>+<Explanation>[Detailed explanation of each improvement made, with before/after comparisons highlighted]</Explanation>++Example:+<Improved>Original text with improvements</Improved>+<Explanation>Original text with explanations of improvements</Explanation>+"#.to_string()+ }++ fn support_format(&self) -> bool {+ false+ }++ fn support_reasoning(&self) -> bool {+ true+ }+}++// SpellingGrammarWriter implementation+pub struct SpellingGrammarWriter {+ context: CompletionWriterContext,+}++impl SpellingGrammarWriter {+ pub fn new(context: CompletionWriterContext) -> Self {+ SpellingGrammarWriter { context }+ }+}++impl CompletionWriter for SpellingGrammarWriter {+ fn system_message(&self) -> String {+ r#"You excel at identifying and correcting grammatical and spelling errors, ensuring accuracy and professionalism in the text. Provide only the final corrected version without additional commentary, and ensure that your final output maintains the same format and language as the user input."#.to_string()+ }++ fn reasoning_system_message(&self) -> String {+ if let Some(prompt) = &self.context.custom_system_reasoning_prompt {+ return prompt.clone();+ }++ r#"You excel at identifying and correcting grammatical and spelling errors while maintaining the original text's meaning and style. Your task is to provide corrections and explanations for any text I submit.+For each submission, please:++1. Provide the corrected version of the text+2. Explain the corrections made:+ - Identify specific grammatical and spelling issues+ - Use markdown formatting (**bold** or `code`) to highlight problem areas+ - If no errors exist, simply confirm the text is correct+3. Preserve the original format, style, and language++Format your response like this:+<Corrected>[The fully corrected text]</Corrected>+<Explanation>[Clear explanation of each correction made, with examples highlighted]</Explanation>++Example:+<Corrected>Original text with corrections</Corrected>+<Explanation>Original text with explanations of corrections</Explanation>++"#.to_string()+ }++ fn support_format(&self) -> bool {+ false+ }++ fn support_reasoning(&self) -> bool {+ true+ }+}++// SummaryWriter implementation+pub struct SummaryWriter {+ #[allow(dead_code)]+ context: CompletionWriterContext,+}++impl SummaryWriter {+ pub fn new(context: CompletionWriterContext) -> Self {+ SummaryWriter { context }+ }+}++impl CompletionWriter for SummaryWriter {+ fn system_message(&self) -> String {+ r#"You are skilled at summarizing text, capturing its core message in a concise, clear manner without losing its original intent. Ensure that your final output retains the same format and language as the user input."#.to_string()+ }++ fn support_format(&self) -> bool {+ true+ }+}++// MakeLongerWriter implementation+pub struct MakeLongerWriter {+ #[allow(dead_code)]+ context: CompletionWriterContext,+}++impl MakeLongerWriter {+ pub fn new(context: CompletionWriterContext) -> Self {+ MakeLongerWriter { context }+ }+}++impl CompletionWriter for MakeLongerWriter {+ fn system_message(&self) -> String {+ r#"You are skilled at expanding and elaborating on existing content, enriching it with additional details while maintaining the original meaning. Ensure that your final output maintains the same format and language as the user input."#.to_string()+ }++ fn support_format(&self) -> bool {+ true+ }+}++// ContinueWriteWriter implementation+pub struct ContinueWriteWriter {+ #[allow(dead_code)]+ context: CompletionWriterContext,+}++impl ContinueWriteWriter {+ pub fn new(context: CompletionWriterContext) -> Self {+ ContinueWriteWriter { context }+ }+}++impl CompletionWriter for ContinueWriteWriter {+ fn system_message(&self) -> String {+ r#"You are the AppFlowyAI writing assistant. Treat the user's latest input as the foundational context.+Analyze its themes, style, tone, and key ideas, then extend the content with new, relevant details and+logical elaborations, ensuring coherence and consistency. Ensure that your final output maintains the+same format and language as the user input."#+ .to_string()+ }++ fn support_format(&self) -> bool {+ true+ }+}++// ExplainWriter implementation+pub struct ExplainWriter {+ #[allow(dead_code)]+ context: CompletionWriterContext,+}++impl ExplainWriter {+ pub fn new(context: CompletionWriterContext) -> Self {+ ExplainWriter { context }+ }+}++impl CompletionWriter for ExplainWriter {+ fn system_message(&self) -> String {+ r#"You are an expert educator and communicator who excels at making complex ideas accessible. Your task is+to analyze and explain any concept or text I share with you.+When I provide text, please:++1. Identify the core concept, idea, or message+2. Break it down into clear, digestible components+3. Provide a comprehensive yet concise explanation+4. Include relevant examples, analogies, or illustrations that clarify the concept+5. Match your explanation to my level of understanding based on my query+6. Respond in the same language as my input++Your explanation should:+- Be easy to understand without oversimplifying+- Highlight key themes and important details+- Connect the concept to broader knowledge when helpful+- Be organized in a logical progression+- Preserve the nuance and depth of the original idea++Focus on delivering a thoughtful explanation that would help someone truly grasp the concept rather than+just rephrasing the input."#+ .to_string()+ }++ fn support_format(&self) -> bool {+ true+ }+}++// AskAiWriter implementation+pub struct AskAiWriter {+ #[allow(dead_code)]+ context: CompletionWriterContext,+}++impl AskAiWriter {+ pub fn new(context: CompletionWriterContext) -> Self {+ AskAiWriter { context }+ }+}++impl CompletionWriter for AskAiWriter {+ fn system_message(&self) -> String {+ r#"You are AppFlowy AI, adept at understanding and answering user inquiries quickly and accurately.+Provide clear, concise responses that effectively resolve the query, and ensure your final output is in the+same language as the user input."#+ .to_string()+ }++ fn support_format(&self) -> bool {+ true+ }+}++// CustomWriter implementation for user-provided prompts+pub struct CustomWriter {+ #[allow(dead_code)]+ context: CompletionWriterContext,+ prompt: CustomPrompt,+}++impl CustomWriter {+ pub fn new(prompt: CustomPrompt, context: CompletionWriterContext) -> Self {+ CustomWriter { context, prompt }+ }+}++impl CompletionWriter for CustomWriter {+ fn system_message(&self) -> String {+ self.prompt.system.clone()+ }++ fn support_format(&self) -> bool {+ true+ }+}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs b/frontend/rust-lib/flowy-ai/src/local_ai/controller.rsindex 53e551bf5f..80e43678ad 100644--- a/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs+++ b/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs@@ -1,9 +1,8 @@-use crate::entities::{LocalAIPB, RunningStatePB};+use crate::entities::LocalAIPB;use crate::local_ai::resource::{LLMResourceService, LocalAIResourceController};use crate::notification::{chat_notification_builder, ChatNotification, APPFLOWY_AI_NOTIFICATION_KEY,};-use af_plugin::manager::PluginManager;use anyhow::Error;use flowy_error::{FlowyError, FlowyResult};use flowy_sqlite::kv::KVStorePreferences;@@ -11,10 +10,8 @@ use futures::Sink;use lib_infra::async_trait::async_trait;use std::collections::HashMap;+use crate::local_ai::chat::LLMChatController;use crate::stream_message::StreamMessage;-use af_local_ai::ollama_plugin::OllamaAIPlugin;-use af_plugin::core::path::is_plugin_ready;-use af_plugin::core::plugin::RunningState;use arc_swap::ArcSwapOption;use flowy_ai_pub::cloud::AIModel;use flowy_ai_pub::persistence::{@@ -29,9 +26,7 @@ use serde::{Deserialize, Serialize};use std::ops::Deref;use std::path::PathBuf;use std::sync::{Arc, Weak};-use tokio::select;-use tokio_stream::StreamExt;-use tracing::{debug, error, info, instrument, warn};+use tracing::{debug, error, info, instrument};use uuid::Uuid;#[derive(Clone, Debug, Serialize, Deserialize)]@@ -54,7 +49,7 @@ impl Default for LocalAISetting {const LOCAL_AI_SETTING_KEY: &str = "appflowy_local_ai_setting:v1";pub struct LocalAIController {- ai_plugin: Arc<OllamaAIPlugin>,+ llm_controller: LLMChatController,resource: Arc<LocalAIResourceController>,current_chat_id: ArcSwapOption<Uuid>,store_preferences: Weak<KVStorePreferences>,@@ -63,16 +58,15 @@ pub struct LocalAIController {}impl Deref for LocalAIController {- type Target = Arc<OllamaAIPlugin>;+ type Target = LLMChatController;fn deref(&self) -> &Self::Target {- &self.ai_plugin+ &self.llm_controller}}impl LocalAIController {pub fn new(- plugin_manager: Arc<PluginManager>,store_preferences: Weak<KVStorePreferences>,user_service: Arc<dyn AIUserService>,) -> Self {@@ -82,7 +76,6 @@ impl LocalAIController {);// Create the core plugin and resource controller- let local_ai = Arc::new(OllamaAIPlugin::new(plugin_manager));let res_impl = LLMResourceServiceImpl {store_preferences: store_preferences.clone(),};@@ -92,75 +85,9 @@ impl LocalAIController {));let ollama = ArcSwapOption::default();-- let sys = get_operating_system();- if sys.is_desktop() {- // Subscribe to state changes- let mut running_state_rx = local_ai.subscribe_running_state();- let cloned_llm_res = Arc::clone(&local_ai_resource);- let cloned_store_preferences = store_preferences.clone();- let cloned_local_ai = Arc::clone(&local_ai);- let cloned_user_service = Arc::clone(&user_service);-- // Spawn a background task to listen for plugin state changes- tokio::spawn(async move {- while let Some(state) = running_state_rx.next().await {- // Skip if we can't get workspace_id- let Ok(workspace_id) = cloned_user_service.workspace_id() else {- continue;- };-- let key = local_ai_enabled_key(&workspace_id.to_string());- info!("[Local AI] state: {:?}", state);-- // Read whether plugin is enabled from store; default to true- if let Some(store_preferences) = cloned_store_preferences.upgrade() {- let enabled = store_preferences.get_bool(&key).unwrap_or(true);- // Only check resource status if the plugin isn't in "UnexpectedStop" and is enabled- let (plugin_downloaded, lack_of_resource) =- if !matches!(state, RunningState::UnexpectedStop { .. }) && enabled {- // Possibly check plugin readiness and resource concurrency in parallel,- // but here we do it sequentially for clarity.- let downloaded = is_plugin_ready();- let resource_lack = cloned_llm_res.get_lack_of_resource().await;- (downloaded, resource_lack)- } else {- (false, None)- };-- // If plugin is running, retrieve version- let plugin_version = if matches!(state, RunningState::Running { .. }) {- match cloned_local_ai.plugin_info().await {- Ok(info) => Some(info.version),- Err(_) => None,- }- } else {- None- };-- // Broadcast the new local AI state- let new_state = RunningStatePB::from(state);- chat_notification_builder(- APPFLOWY_AI_NOTIFICATION_KEY,- ChatNotification::UpdateLocalAIState,- )- .payload(LocalAIPB {- enabled,- plugin_downloaded,- lack_of_resource,- state: new_state,- plugin_version,- })- .send();- } else {- warn!("[Local AI] store preferences is dropped");- }- }- });- }-+ let llm_controller = LLMChatController::new(Arc::downgrade(&user_service));Self {- ai_plugin: local_ai,+ llm_controller,resource: local_ai_resource,current_chat_id: ArcSwapOption::default(),store_preferences,@@ -169,7 +96,7 @@ impl LocalAIController {}}- pub fn reload_ollama_client(&self, workspace_id: &str) {+ pub async fn reload_ollama_client(&self, workspace_id: &str) {if !self.is_enabled_on_workspace(workspace_id) {return;}@@ -183,63 +110,32 @@ impl LocalAIController {}info!("[Local AI] reloading ollama client");- match Ollama::try_new(setting.ollama_server_url).map(Arc::new) {+ match Ollama::try_new(&setting.ollama_server_url).map(Arc::new) {Ok(new_ollama) => {- self.ollama.store(Some(new_ollama.clone()));-#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]- crate::embeddings::context::EmbedContext::shared().set_ollama(Some(new_ollama.clone()));+ {+ info!("[Local AI] reload ollama client successfully");+ let shared = crate::embeddings::context::EmbedContext::shared();+ shared.set_ollama(Some(new_ollama.clone()));+ if let Some(vc) = shared.get_vector_db() {+ self+ .llm_controller+ .initialize(Arc::downgrade(&new_ollama), Arc::downgrade(&vc))+ .await;+ } else {+ error!("[Local AI] vector db is not initialized");+ }+ }+ self.ollama.store(Some(new_ollama.clone()));},Err(err) => error!(- "failed to create ollama client: {:?}, thread: {:?}",+ "[Local AI] failed to create ollama client: {:?}, thread: {:?}",err,std::thread::current().id()),}}- #[instrument(level = "debug", skip_all)]- pub async fn observe_plugin_resource(&self) {- let sys = get_operating_system();- if !sys.is_desktop() {- return;- }-- debug!(- "[Local AI] observer plugin state. thread: {:?}",- std::thread::current().id()- );- 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!("[Local AI] 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! {- _ = app_state_watcher.recv() => {- info!("[Local AI] app state changed, try to init plugin");- try_init_plugin(&resource_clone, &ai_plugin_clone).await;- },- _ = resource_notify.recv() => {- info!("[Local AI] resource changed, try to init plugin");- try_init_plugin(&resource_clone, &ai_plugin_clone).await;- },- else => break,- }- }- });- }-fn upgrade_store_preferences(&self) -> FlowyResult<Arc<KVStorePreferences>> {self.store_preferences@@ -247,11 +143,6 @@ impl LocalAIController {.ok_or_else(|| FlowyError::internal().with_context("Store preferences is dropped"))}- /// Indicate whether the local AI plugin is running.- pub fn is_running(&self) -> bool {- self.ai_plugin.get_plugin_running_state().is_running()- }-/// Indicate whether the local AI is enabled./// AppFlowy store the value in local storage isolated by workspace id. Each workspace can have/// different settings.@@ -279,16 +170,21 @@ impl LocalAIController {}}- pub fn get_plugin_chat_model(&self) -> Option<String> {+ pub fn get_local_chat_model(&self) -> Option<String> {if !self.is_enabled() {return None;}Some(self.resource.get_llm_setting().chat_model_name)}- pub fn open_chat(&self, chat_id: &Uuid) {+ pub async fn open_chat(+ &self,+ workspace_id: &Uuid,+ chat_id: &Uuid,+ model: &str,+ ) -> FlowyResult<()> {if !self.is_enabled() {- return;+ return Ok(());}// Only keep one chat open at a time. Since loading multiple models at the same time will cause@@ -299,31 +195,16 @@ impl LocalAIController {}self.current_chat_id.store(Some(Arc::new(*chat_id)));- let chat_id = chat_id.to_string();- 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 {- error!("[Local AI] failed to open chat: {:?}", err);- }- }- });+ self+ .llm_controller+ .open_chat(workspace_id, chat_id, model)+ .await?;+ Ok(())}pub fn close_chat(&self, chat_id: &Uuid) {- if !self.is_running() {- return;- }info!("[Local AI] notify close chat: {}", chat_id);- 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() {- if let Err(err) = ctrl.close_chat(&chat_id).await {- error!("[Local AI] failed to close chat: {:?}", err);- }- }- });+ self.llm_controller.close_chat(chat_id);}pub fn get_local_ai_setting(&self) -> LocalAISetting {@@ -414,61 +295,25 @@ impl LocalAIController {#[instrument(level = "debug", skip_all)]pub async fn get_local_ai_state(&self) -> LocalAIPB {- let start = std::time::Instant::now();let enabled = self.is_enabled();-- // If not enabled, return immediately.if !enabled {- debug!(- "[Local AI] get local ai state, elapsed: {:?}, thread: {:?}",- start.elapsed(),- std::thread::current().id()- );return LocalAIPB {enabled,- plugin_downloaded: false,- state: RunningStatePB::from(RunningState::ReadyToConnect),lack_of_resource: None,- plugin_version: None,+ is_ready: self.is_ready().await,};}-- let plugin_downloaded = is_plugin_ready();- let state = self.ai_plugin.get_plugin_running_state();-- // If the plugin is running, run both requests in parallel.- // Otherwise, only fetch the resource info.- let (plugin_version, lack_of_resource) = if matches!(state, RunningState::Running { .. }) {- // Launch both futures at once- let plugin_info_fut = self.ai_plugin.plugin_info();- let resource_fut = self.resource.get_lack_of_resource();-- let (plugin_info_res, resource_res) = tokio::join!(plugin_info_fut, resource_fut);- let plugin_version = plugin_info_res.ok().map(|info| info.version);- (plugin_version, resource_res)- } else {- let resource_res = self.resource.get_lack_of_resource().await;- (None, resource_res)- };-- let elapsed = start.elapsed();- debug!(- "[Local AI] get local ai state, elapsed: {:?}, thread: {:?}",- elapsed,- std::thread::current().id()- );-+ let lack_of_resource = self.resource.get_lack_of_resource().await;LocalAIPB {enabled,- plugin_downloaded,- state: RunningStatePB::from(state),lack_of_resource,- plugin_version,+ is_ready: self.is_ready().await,}}+#[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 {+ if let Err(err) = check_local_ai_resources(&self.resource, &self.llm_controller).await {error!("[Local AI] failed to setup plugin: {:?}", err);}}@@ -567,12 +412,8 @@ impl LocalAIController {.await;let result = self- .ai_plugin- .embed_file(- &chat_id.to_string(),- file_path,- Some(index_metadata.clone()),- )+ .llm_controller+ .embed_file(chat_id, file_path, Some(index_metadata.clone())).await;match result {Ok(_) => {@@ -599,26 +440,18 @@ impl LocalAIController {std::thread::current().id());if enabled {- let (tx, rx) = tokio::sync::oneshot::channel();- if let Err(err) = initialize_ai_plugin(&self.ai_plugin, &self.resource, Some(tx)).await {+ if let Err(err) = check_local_ai_resources(&self.resource, &self.llm_controller).await {error!("[Local AI] failed to initialize local ai: {:?}", err);}- let _ = rx.await;} else {- if let Err(err) = self.ai_plugin.destroy_plugin().await {- error!("[Local AI] failed to destroy plugin: {:?}", err);- }-chat_notification_builder(APPFLOWY_AI_NOTIFICATION_KEY,ChatNotification::UpdateLocalAIState,).payload(LocalAIPB {enabled,- plugin_downloaded: true,- state: RunningStatePB::Stopped,lack_of_resource: None,- plugin_version: None,+ is_ready: self.is_ready().await,}).send();}@@ -627,10 +460,9 @@ impl LocalAIController {}#[instrument(level = "debug", skip_all, err)]-async fn initialize_ai_plugin(- plugin: &Arc<OllamaAIPlugin>,+async fn check_local_ai_resources(llm_resource: &Arc<LocalAIResourceController>,- ret: Option<tokio::sync::oneshot::Sender<()>>,+ llm_controller: &LLMChatController,) -> FlowyResult<()> {let lack_of_resource = llm_resource.get_lack_of_resource().await;@@ -640,10 +472,8 @@ async fn initialize_ai_plugin().payload(LocalAIPB {enabled: true,- plugin_downloaded: true,- state: RunningStatePB::ReadyToRun,lack_of_resource: lack_of_resource.clone(),- plugin_version: None,+ is_ready: llm_controller.is_ready().await,}).send();@@ -663,41 +493,6 @@ async fn initialize_ai_plugin(return Ok(());}- if let Err(err) = plugin.destroy_plugin().await {- error!(- "[Local AI] failed to destroy plugin when lack of resource: {:?}",- err- );- }-- let plugin = plugin.clone();- 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!(- "[Local AI] initialize plugin with config: {:?}, thread: {:?}",- config,- std::thread::current().id()- );-- match plugin.init_plugin(config).await {- Ok(_) => {},- Err(err) => error!("[Local AI] failed to setup plugin: {:?}", err),- }-- if let Some(ret) = ret {- let _ = ret.send(());- }- },- Err(err) => {- error!("[Local AI] failed to get plugin config: {:?}", err);- },- };- })- });-Ok(())}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/database/mod.rs b/frontend/rust-lib/flowy-ai/src/local_ai/database/mod.rsnew file mode 100644index 0000000000..fe18831d6a--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/database/mod.rs@@ -0,0 +1,2 @@+pub mod summary;+pub mod translate;diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/database/summary.rs b/frontend/rust-lib/flowy-ai/src/local_ai/database/summary.rsnew file mode 100644index 0000000000..b34dc6943d--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/database/summary.rs@@ -0,0 +1,73 @@+use crate::local_ai::chat::llm::LLMOllama;+use flowy_database_pub::cloud::SummaryRowContent;+use flowy_error::{FlowyError, FlowyResult};+use langchain_rust::language_models::llm::LLM;+use langchain_rust::schemas::Message;++const SUMMARIZE_SYSTEM_PROMPT: &str = r#"+You are AppFlowy AI who are good at distilling complex information into clear and concise summaries. When summarizing, please ensure that the main ideas are communicated effectively and efficiently.+The input text consists of two parts: a category and its corresponding content. Summarize the content based on its category, but do not include the category in the output. Additionally, consider the relationships between categories and summarize the content based on these relationships+{good_format_example}++When summarizing, please follow these constraint+1. If a category's content exceeds 500 characters, shorten it+2. If you can't recognize a category's content, ignore it+3. Do not add any content that is not included in the original sentence+"#;++const SUMMARIZE_USER_PROMPT: &str = r#"+Summarize following sentence: {input}+Output:+"#;++const SUMMARY_GOOD_FORMAT_EXAMPLE: &str = r#"+Here are examples of correctly formatted outputs++Example+Input: description: Card 1\n content: To do+Output: Card 1 is currently in the "To do" status+"#;++pub struct DatabaseSummaryChain {+ llm: LLMOllama,+}++impl DatabaseSummaryChain {+ pub fn new(llm: LLMOllama) -> Self {+ Self { llm }+ }++ pub async fn summarize(&self, data: SummaryRowContent) -> FlowyResult<String> {+ let input_text = self.format_summary_data(&data);+ self.summarize_text(input_text).await+ }++ fn format_summary_data(&self, data: &SummaryRowContent) -> String {+ let mut formatted_items = Vec::new();+ for (key, value) in data.iter() {+ let value_str = value.to_string().trim().to_string();+ if !value_str.is_empty() {+ formatted_items.push(format!("{}:{}", key, value_str));+ }+ }++ formatted_items.join("\n")+ }++ async fn summarize_text(&self, text: String) -> FlowyResult<String> {+ let system_prompt =+ SUMMARIZE_SYSTEM_PROMPT.replace("{good_format_example}", SUMMARY_GOOD_FORMAT_EXAMPLE);+ let user_prompt = SUMMARIZE_USER_PROMPT.replace("{input}", &text);++ let system_prompt = Message::new_system_message(system_prompt);+ let user_prompt = Message::new_human_message(user_prompt);+ let messages = vec![system_prompt, user_prompt];++ match self.llm.generate(&messages).await {+ Ok(response) => Ok(response.generation),+ Err(err) => {+ Err(FlowyError::internal().with_context(format!("Error generating summary: {}", err)))+ },+ }+ }+}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/database/translate.rs b/frontend/rust-lib/flowy-ai/src/local_ai/database/translate.rsnew file mode 100644index 0000000000..85eddf2b21--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/database/translate.rs@@ -0,0 +1,217 @@+use anyhow::Result;+use std::collections::HashMap;++use crate::local_ai::chat::llm::LLMOllama;+use flowy_ai_pub::cloud::ai_dto::{TranslateRowData, TranslateRowResponse};+use flowy_database_pub::cloud::TranslateItem;+use flowy_error::{FlowyError, FlowyResult};+use langchain_rust::language_models::llm::LLM;+use langchain_rust::schemas::Message;+use tracing::{info, warn};++/// The translation prompt templates+const TRANSLATE_SYSTEM_PROMPT: &str = r#"+You are AppFlowy AI who fluent in multiple languages, able to translate texts accurately and efficiently between different languages while maintaining the original context and meaning++<Examples>+-----------------+Translate to Chinese+Input: [{"name": "jack"}, {"age": "twelve"}, {"location": "New York"}]+Output: [{"姓名": "杰克"}, {"年龄": "12"}, {"位置": "纽约"}]++Translate to French+Input: [{"name": "杰克"}, {"age": "12"}, {"地点": "纽约"}]+Output: [{"nom": "Jacques"}, {"âge": "douze"}, {"lieu": "New York"}]++Translate to German+Input: [{"name": "杰克"}, {"age": "12"}, {"地点": "纽约"}]+Output: [{"name": "Jacques"}, {"alter": "fünfundzwanzig"}, {"stadt": "New York"}]+</Examples>++Important Formatting Rules:+- The output must be a JSON array of key–value maps.+- Each key and each string value must be enclosed in quotes (e.g., "name": "Jack").+- Numeric values should not be enclosed in quotes (e.g., "age": 25).+- Do not include any markdown formatting (such as ```json or ```).+- The output must be directly parseable as JSON.+"#;++const TRANSLATE_USER_PROMPT: &str = r#"+Translate following {input} into {language}.+Output:+"#;++/// The main chain for translating database content+pub struct DatabaseTranslateChain {+ llm: LLMOllama,+}++impl DatabaseTranslateChain {+ pub fn new(ollama: LLMOllama) -> Self {+ Self { llm: ollama }+ }++ /// Translates the provided data to the specified language+ pub async fn translate(&self, data: TranslateRowData) -> FlowyResult<TranslateRowResponse> {+ // Format the input text+ let text: Vec<String> = data+ .cells+ .iter()+ .map(|item| format!("{}: {}", item.title, item.content))+ .collect();++ // Create the prompt messages+ let input_json = serde_json::to_string(&text).unwrap_or_else(|_| text.join("\n"));+ let messages = vec![+ Message::new_system_message(TRANSLATE_SYSTEM_PROMPT),+ Message::new_human_message(+ TRANSLATE_USER_PROMPT+ .replace("{input}", &input_json)+ .replace("{language}", &data.language),+ ),+ ];++ self+ .llm+ .generate(&messages)+ .await+ .map_err(|err| {+ FlowyError::internal().with_context(format!("Error generating translation: {}", err))+ })+ .and_then(|result| self.parse_response(&result.generation))+ }++ /// Parses the LLM response into the expected output format+ fn parse_response(&self, response_text: &str) -> FlowyResult<TranslateRowResponse> {+ info!("[Database Translate] Parsing response: {}", response_text);++ // Try different parsing strategies in sequence+ self+ .try_parse_json_array(response_text)+ .or_else(|_| self.try_parse_json_object(response_text))+ .or_else(|_| self.fallback_parse(response_text))+ .map_err(|e| {+ FlowyError::internal().with_context(format!("Failed to parse translation response: {}", e))+ })+ }++ // Try to parse the response as a JSON array+ fn try_parse_json_array(&self, text: &str) -> Result<TranslateRowResponse, anyhow::Error> {+ // Directly attempt JSON parsing and handle any errors+ let text = text.trim();+ if !text.starts_with('[') {+ return Err(anyhow::anyhow!("Not a JSON array"));+ }++ serde_json::from_str::<Vec<serde_json::Value>>(text)+ .map(|parsed| self.convert_values_to_response(parsed))+ .map_err(|e| anyhow::anyhow!("Failed to parse JSON array: {}", e))+ }++ // Try to parse the response as a JSON object+ fn try_parse_json_object(&self, text: &str) -> Result<TranslateRowResponse, anyhow::Error> {+ let text = text.trim();+ if !text.starts_with('{') {+ return Err(anyhow::anyhow!("Not a JSON object"));+ }++ let parsed: serde_json::Value = serde_json::from_str(text)+ .map_err(|e| anyhow::anyhow!("Failed to parse JSON object: {}", e))?;++ // Check if it's an object with an "items" field+ if let Some(serde_json::Value::Array(items)) = parsed.get("items") {+ return Ok(self.convert_values_to_response(items.clone()));+ }++ // Otherwise, treat as a flat key-value object+ if let serde_json::Value::Object(map) = parsed {+ let result = map+ .into_iter()+ .map(|(k, v)| {+ let content = match v {+ serde_json::Value::String(s) => s,+ _ => v.to_string(),+ };+ TranslateItem { title: k, content }+ })+ .collect();++ return Ok(map_translate_items_to_response(result));+ }++ Err(anyhow::anyhow!("JSON value is not an object"))+ }++ // Fallback parsing for non-JSON or malformed responses+ fn fallback_parse(&self, text: &str) -> Result<TranslateRowResponse, anyhow::Error> {+ warn!("[Database Translate] Using fallback parsing for: {}", text);++ // Try to parse line by line+ let items = text+ .lines()+ .filter_map(|line| {+ let line = line.trim();+ if line.is_empty() {+ return None;+ }++ let parts: Vec<&str> = line.splitn(2, ": ").collect();+ (parts.len() == 2).then(|| TranslateItem {+ title: parts[0].trim().to_string(),+ content: parts[1].trim().to_string(),+ })+ })+ .collect();++ Ok(map_translate_items_to_response(items))+ }++ // Convert JSON values to TranslateRowResponse+ fn convert_values_to_response(&self, values: Vec<serde_json::Value>) -> TranslateRowResponse {+ let items = values+ .into_iter()+ .filter_map(|value| match value {+ serde_json::Value::Object(map) => {+ if map.len() == 1 {+ // Handle simple key-value object+ map.into_iter().next().map(|(k, v)| {+ let content = match v {+ serde_json::Value::String(s) => s,+ _ => v.to_string(),+ };+ TranslateItem { title: k, content }+ })+ } else {+ // Try to convert the object directly to a TranslateItem+ serde_json::from_value::<TranslateItem>(serde_json::Value::Object(map)).ok()+ }+ },+ serde_json::Value::String(s) => {+ // Handle string values with "key: value" format+ let parts: Vec<&str> = s.splitn(2, ": ").collect();+ (parts.len() == 2).then(|| TranslateItem {+ title: parts[0].trim().to_string(),+ content: parts[1].trim().to_string(),+ })+ },+ _ => None,+ })+ .collect();++ map_translate_items_to_response(items)+ }+}++fn map_translate_items_to_response(items: Vec<TranslateItem>) -> TranslateRowResponse {+ TranslateRowResponse {+ items: items+ .into_iter()+ .map(|item| {+ let mut map = HashMap::with_capacity(2);+ map.insert("title".to_string(), item.title);+ map.insert("content".to_string(), item.content);+ map+ })+ .collect(),+ }+}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/mod.rs b/frontend/rust-lib/flowy-ai/src/local_ai/mod.rsindex c0fd967d43..05a9cad849 100644--- a/frontend/rust-lib/flowy-ai/src/local_ai/mod.rs+++ b/frontend/rust-lib/flowy-ai/src/local_ai/mod.rs@@ -2,5 +2,9 @@ pub mod controller;mod request;pub mod resource;+pub mod chat;+pub mod completion;+pub mod database;+pub mod prompt;pub mod stream_util;pub mod watch;diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/prompt/mod.rs b/frontend/rust-lib/flowy-ai/src/local_ai/prompt/mod.rsnew file mode 100644index 0000000000..52551be801--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/local_ai/prompt/mod.rs@@ -0,0 +1,81 @@+use flowy_ai_pub::cloud::{CompletionMessage, OutputLayout, ResponseFormat};+use langchain_rust::schemas::{Message, MessageType};++pub const OPEN_AI_QA_FLEX_FORMAT: &str = r#"Use Markdown for formatting. Start responses naturally—avoid formal phrases like “Certainly,” “Absolutely,” or “Sure.” Keep the tone conversational, clear, and professional."#;++const OPEN_AI_QA_PARAGRAPH_FORMAT: &str = r#"Respond naturally in one paragraph. Avoid formal phrases like “Certainly,” “Absolutely,” or “Sure.” Keep the tone conversational, clear, and professional."#;++const OPEN_AI_QA_BULLET_LIST_FORMAT: &str = r#"Organize the provided information into a natural, practical, and well-structured response using a bullet list format. Follow these guidelines:+1. Begin your response with a brief description of the content.+2. Use `*` to denote each bullet point and ensure each point is clear, informative, and relevant.+3. Avoid overly formal language. Present information in a tone and style that people commonly use in discussions or comparisons.+4. Ensure the response is well-structured without unnecessary spaces or formatting issues.+5. Optional Context: Include a brief description or context (1–2 sentences) at the end, if necessary, to summarize or provide additional insights."#;++const OPEN_AI_QA_NUMBER_LIST_FORMAT: &str = r#"Organize the provided information into a natural, practical, and well-structured response using a numbered list format. Follow these guidelines:+1. Begin your response with a brief description of the content.+2. Use numbers (e.g., 1., 2., 3.) to denote each point and ensure they are clear, sequential, and easy to understand.+3. Avoid overly formal language. Present information in a tone and style that people commonly use in discussions or comparisons.+4. Avoid adding unnecessary spaces or formatting issues.+5. Optional Context: Include a brief description or context (1–2 sentences) at the end, if necessary, to summarize or provide additional insights."#;++const OPEN_AI_QA_SIMPLE_TABLE_FORMAT: &str = r#"Organize the provided information into a natural, practical, and well-structured table using Markdown format. Follow these guidelines:+1. Purpose and Clarity: Ensure the table is designed to be useful and relatable for the intended audience (e.g., developers, team members, or general readers).+2. Structure: Use concise headers and rows that highlight the most relevant details without unnecessary complexity.+3. Natural Style: Avoid overly formal language. Present information in a tone and style that people commonly use in discussions or comparisons.+4. Readability: Keep the table visually clean, with evenly spaced columns and clear separation between rows.+5. Optional Context: Include a brief description or context (1–2 sentences) before or after the table to summarize its purpose or provide additional insights.++Example:+input: Iconic Singapore Landmarks+output:+Singapore’s landmarks blend modern architecture with natural beauty. Here’s an overview of its most iconic attractions:+| Attraction | Description |+|--------------------|-----------------------------------------------------|+| Marina Bay Sands | Iconic resort with a rooftop infinity pool. |+| Gardens by the Bay | Futuristic nature park featuring Supertree Grove. |+| Merlion Park | Home to the iconic Merlion statue. |+| Sentosa Island | Beaches, theme parks, and Universal Studios Singapore. |++These attractions highlight Singapore’s blend of modernity and greenery, offering visitors diverse experiences."#;++pub fn format_prompt(format: &ResponseFormat) -> Message {+ match format.output_layout {+ OutputLayout::Paragraph => Message::new_system_message(OPEN_AI_QA_PARAGRAPH_FORMAT),+ OutputLayout::BulletList => Message::new_system_message(OPEN_AI_QA_BULLET_LIST_FORMAT),+ OutputLayout::NumberedList => Message::new_system_message(OPEN_AI_QA_NUMBER_LIST_FORMAT),+ OutputLayout::SimpleTable => Message::new_system_message(OPEN_AI_QA_SIMPLE_TABLE_FORMAT),+ OutputLayout::Flex => Message::new_system_message(OPEN_AI_QA_FLEX_FORMAT),+ }+}++pub fn history_prompt(history: Option<Vec<CompletionMessage>>) -> Vec<Message> {+ let mut messages = vec![];+ if let Some(history) = history {+ for message in history {+ if let Ok(message_type) = serde_json::from_str::<MessageType>(&message.role) {+ messages.push(Message {+ content: message.content,+ message_type,+ id: None,+ tool_calls: None,+ images: None,+ });+ }+ }+ }++ if !messages.is_empty() {+ let content = r#"+ Analyze the conversation history to identify the core question or request in the user's most recent message.+Then:+1. Silently reframe the complete question in your mind+2. Deliver your answer directly without introductory phrases such as "Here is", "Certainly", "Here's the translation", etc.+3. Maintain the established conversation tone while providing thorough information+4. Begin your response immediately with the relevant content the user is seeking+Provide precise, helpful answers without meta-commentary about your interpretation process or unnecessary lead-in phrases.+ "#;+ messages.insert(0, Message::new_system_message(content));+ }+ messages+}diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/resource.rs b/frontend/rust-lib/flowy-ai/src/local_ai/resource.rsindex 352778f28f..3f091f69a7 100644--- a/frontend/rust-lib/flowy-ai/src/local_ai/resource.rs+++ b/frontend/rust-lib/flowy-ai/src/local_ai/resource.rs@@ -3,8 +3,6 @@ use flowy_error::{ErrorCode, FlowyError, FlowyResult};use lib_infra::async_trait::async_trait;use crate::entities::LackOfAIResourcePB;-#[cfg(any(target_os = "macos", target_os = "linux"))]-use crate::local_ai::watch::{watch_offline_app, WatchContext};use crate::notification::{chat_notification_builder, ChatNotification, APPFLOWY_AI_NOTIFICATION_KEY,};@@ -55,11 +53,6 @@ pub enum PendingResource {pub struct LocalAIResourceController {user_service: Arc<dyn AIUserService>,resource_service: Arc<dyn LLMResourceService>,- resource_notify: tokio::sync::broadcast::Sender<()>,- #[cfg(any(target_os = "macos", target_os = "linux"))]- #[allow(dead_code)]- app_disk_watch: Option<WatchContext>,- app_state_sender: tokio::sync::broadcast::Sender<WatchDiskEvent>,}impl LocalAIResourceController {@@ -67,49 +60,12 @@ impl LocalAIResourceController {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(any(target_os = "macos", target_os = "linux"))]- let mut offline_app_disk_watch: Option<WatchContext> = None;-- #[cfg(any(target_os = "macos", target_os = "linux"))]- {- 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(any(target_os = "macos", target_os = "linux"))]- 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();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 67e525730f..0fa92c6649 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,26 +1,18 @@-use crate::entities::{ChatStatePB, ModelTypePB};use crate::local_ai::controller::LocalAIController;-use crate::notification::{- chat_notification_builder, ChatNotification, APPFLOWY_AI_NOTIFICATION_KEY,-};-use af_plugin::error::PluginError;use flowy_ai_pub::persistence::select_message_content;use std::collections::HashMap;use flowy_ai_pub::cloud::{- AIModel, AppErrorCode, AppResponseError, ChatCloudService, ChatMessage, ChatMessageType,- ChatSettings, CompleteTextParams, CompletionStream, MessageCursor, ModelList, RelatedQuestion,- RepeatedChatMessage, RepeatedRelatedQuestion, ResponseFormat, StreamAnswer, StreamComplete,- UpdateChatParams,+ AIModel, ChatCloudService, ChatMessage, ChatMessageType, ChatSettings, CompleteTextParams,+ MessageCursor, ModelList, RelatedQuestion, RepeatedChatMessage, RepeatedRelatedQuestion,+ ResponseFormat, StreamAnswer, StreamComplete, UpdateChatParams,};use flowy_error::{FlowyError, FlowyResult};-use futures::{stream, StreamExt, TryStreamExt};use lib_infra::async_trait::async_trait;-use crate::local_ai::stream_util::QuestionStream;use flowy_ai_pub::user_service::AIUserService;use flowy_storage_pub::storage::StorageService;-use serde_json::{json, Value};+use serde_json::Value;use std::path::Path;use std::sync::{Arc, Weak};use tracing::{info, trace};@@ -57,23 +49,6 @@ impl ChatServiceMiddleware {})?;Ok(content)}-- fn handle_plugin_error(&self, err: PluginError) {- if matches!(- err,- PluginError::PluginNotConnected | PluginError::PeerDisconnect- ) {- chat_notification_builder(- APPFLOWY_AI_NOTIFICATION_KEY,- ChatNotification::UpdateLocalAIState,- )- .payload(ChatStatePB {- model_type: ModelTypePB::LocalAI,- available: false,- })- .send();- }- }}#[async_trait]@@ -135,28 +110,14 @@ impl ChatCloudService for ChatServiceMiddleware {info!("stream_answer use model: {:?}", ai_model);if use_local_ai {- if self.local_ai.is_running() {+ if self.local_ai.is_ready().await {let content = self.get_message_content(question_id)?;- match self+ self.local_ai- .stream_question(- &chat_id.to_string(),- &content,- Some(json!(format)),- json!({}),- )+ .stream_question(chat_id, &content, format).await- {- Ok(stream) => Ok(QuestionStream::new(stream).boxed()),- Err(err) => {- self.handle_plugin_error(err);- Ok(stream::once(async { Err(FlowyError::local_ai_unavailable()) }).boxed())- },- }- } else if self.local_ai.is_enabled() {- Err(FlowyError::local_ai_not_ready())} else {- Err(FlowyError::local_ai_disabled())+ Err(FlowyError::local_ai_not_ready())}} else {self@@ -172,25 +133,15 @@ impl ChatCloudService for ChatServiceMiddleware {chat_id: &Uuid,question_id: i64,) -> Result<ChatMessage, FlowyError> {- if self.local_ai.is_running() {+ if self.local_ai.is_ready().await {let content = self.get_message_content(question_id)?;- match self- .local_ai- .ask_question(&chat_id.to_string(), &content)- .await- {- Ok(answer) => {- let message = self- .cloud_service- .create_answer(workspace_id, chat_id, &answer, question_id, None)- .await?;- Ok(message)- },- Err(err) => {- self.handle_plugin_error(err);- Err(FlowyError::local_ai_unavailable())- },- }+ let answer = self.local_ai.ask_question(chat_id, &content).await?;++ let message = self+ .cloud_service+ .create_answer(workspace_id, chat_id, &answer, question_id, None)+ .await?;+ Ok(message)} else {self.cloud_service@@ -229,22 +180,15 @@ impl ChatCloudService for ChatServiceMiddleware {workspace_id: &Uuid,chat_id: &Uuid,message_id: i64,- ai_model: Option<AIModel>,+ ai_model: AIModel,) -> Result<RepeatedRelatedQuestion, FlowyError> {- let use_local_ai = match &ai_model {- None => false,- Some(model) => model.is_local,- };-- if use_local_ai {- if self.local_ai.is_running() {+ if ai_model.is_local {+ if self.local_ai.is_ready().await {let questions = self.local_ai- .get_related_question(&chat_id.to_string())- .await- .map_err(|err| FlowyError::local_ai().with_context(err))?;+ .get_related_question(&ai_model.name, chat_id, message_id)+ .await?;trace!("LocalAI related questions: {:?}", questions);-let items = questions.into_iter().map(|content| RelatedQuestion {@@ -272,42 +216,14 @@ impl ChatCloudService for ChatServiceMiddleware {&self,workspace_id: &Uuid,params: CompleteTextParams,- ai_model: Option<AIModel>,+ ai_model: AIModel,) -> Result<StreamComplete, FlowyError> {- let use_local_ai = match &ai_model {- None => false,- Some(model) => model.is_local,- };-info!("stream_complete use custom model: {:?}", ai_model);- if use_local_ai {- if self.local_ai.is_running() {- match self- .local_ai- .complete_text_v2(- ¶ms.text,- params.completion_type.unwrap() as u8,- Some(json!(params.format)),- Some(json!(params.metadata)),- )- .await- {- Ok(stream) => Ok(- CompletionStream::new(- stream.map_err(|err| AppResponseError::new(AppErrorCode::Internal, err.to_string())),- )- .map_err(FlowyError::from)- .boxed(),- ),- Err(err) => {- self.handle_plugin_error(err);- Ok(stream::once(async { Err(FlowyError::local_ai_unavailable()) }).boxed())- },- }- } else if self.local_ai.is_enabled() {- Err(FlowyError::local_ai_not_ready())+ if ai_model.is_local {+ if self.local_ai.is_ready().await {+ self.local_ai.complete_text(&ai_model.name, params).await} else {- Err(FlowyError::local_ai_disabled())+ Err(FlowyError::local_ai_not_ready())}} else {self@@ -324,12 +240,11 @@ impl ChatCloudService for ChatServiceMiddleware {chat_id: &Uuid,metadata: Option<HashMap<String, Value>>,) -> Result<(), FlowyError> {- if self.local_ai.is_running() {+ if self.local_ai.is_ready().await {self.local_ai- .embed_file(&chat_id.to_string(), file_path.to_path_buf(), metadata)- .await- .map_err(|err| FlowyError::local_ai().with_context(err))?;+ .embed_file(chat_id, file_path.to_path_buf(), metadata)+ .await?;Ok(())} else {selfdiff --git a/frontend/rust-lib/flowy-ai/src/offline/offline_message_sync.rs b/frontend/rust-lib/flowy-ai/src/offline/offline_message_sync.rsindex 3369095d94..24148b9927 100644--- a/frontend/rust-lib/flowy-ai/src/offline/offline_message_sync.rs+++ b/frontend/rust-lib/flowy-ai/src/offline/offline_message_sync.rs@@ -187,7 +187,7 @@ impl ChatCloudService for AutoSyncChatService {workspace_id: &Uuid,chat_id: &Uuid,message_id: i64,- ai_model: Option<AIModel>,+ ai_model: AIModel,) -> Result<RepeatedRelatedQuestion, FlowyError> {self.cloud_service@@ -199,7 +199,7 @@ impl ChatCloudService for AutoSyncChatService {&self,workspace_id: &Uuid,params: CompleteTextParams,- ai_model: Option<AIModel>,+ ai_model: AIModel,) -> Result<StreamComplete, FlowyError> {self.cloud_servicediff --git a/frontend/rust-lib/flowy-ai/tests/chat_test/mod.rs b/frontend/rust-lib/flowy-ai/tests/chat_test/mod.rsnew file mode 100644index 0000000000..829066d2f4--- /dev/null+++ b/frontend/rust-lib/flowy-ai/tests/chat_test/mod.rs@@ -0,0 +1,156 @@+use crate::setup_log;+use flowy_ai::embeddings::store::SqliteVectorStore;+use flowy_ai::local_ai::chat::llm::LLMOllama;+use flowy_ai::local_ai::chat::llm_chat::LLMChat;+use flowy_ai::local_ai::chat::related_question_chain::RelatedQuestionChain;+use flowy_ai_pub::cloud::{OutputLayout, QuestionStreamValue, ResponseFormat, StreamAnswer};+use flowy_ai_pub::entities::SOURCE_ID;+use flowy_sqlite_vec::db::VectorSqliteDB;+use ollama_rs::Ollama;+use reqwest::Url;+use std::sync::Arc;+use tempfile::tempdir;+use tokio::sync::RwLock;+use tokio_stream::StreamExt;+use uuid::Uuid;++pub struct TestContext {+ ollama: Arc<Ollama>,+ store: SqliteVectorStore,+ #[allow(dead_code)]+ db: Arc<VectorSqliteDB>,+ #[allow(dead_code)]+ vector_store: SqliteVectorStore,+}++impl TestContext {+ pub fn new() -> anyhow::Result<Self> {+ let ollama_url = "http://localhost:11434";++ let url = Url::parse(ollama_url)?;+ let ollama = Arc::new(Ollama::from_url(url.clone()));++ let temp_dir = tempdir()?;+ let db = Arc::new(VectorSqliteDB::new(temp_dir.into_path())?);++ let vector_store = SqliteVectorStore::new(Arc::downgrade(&ollama), Arc::downgrade(&db));++ Ok(Self {+ ollama,+ store: vector_store.clone(),+ db,+ vector_store,+ })+ }++ pub async fn create_chat(&self, rag_ids: Vec<String>) -> LLMChat {+ let workspace_id = Uuid::new_v4();+ let chat_id = Uuid::new_v4();+ let model = "llama3.1";+ let ollama_client = Arc::new(RwLock::new(Some(Arc::downgrade(&self.ollama))));++ LLMChat::new(+ workspace_id,+ chat_id,+ model,+ ollama_client,+ Some(self.store.clone()),+ rag_ids,+ )+ .await+ .unwrap()+ }+}++#[tokio::test]+async fn local_ollama_test_simple_question() {+ let context = TestContext::new().unwrap();+ let mut chat = context.create_chat(vec![]).await;+ let stream = chat+ .stream_question("hello world", Default::default())+ .await+ .unwrap();+ let result = collect_stream(stream).await;+ dbg!(result);+}++#[tokio::test]+async fn local_ollama_test_chat_context_retrieve() {+ let context = TestContext::new().unwrap();+ let mut chat = context.create_chat(vec![]).await;+ let mut ids = vec![];++ for (doc, id) in [("Rust is a multiplayer survival game developed by Facepunch Studios, first released in early access in December 2013 and fully launched in February 2018. It has since become one of the most popular games in the survival genre, known for its harsh environment, intricate crafting system, and player-driven dynamics. The game is available on Windows, macOS, and PlayStation, with a community-driven approach to updates and content additions.", uuid::Uuid::new_v4()),+ ("Rust is a modern, system-level programming language designed with a focus on performance, safety, and concurrency. It was created by Mozilla and first released in 2010, with its 1.0 version launched in 2015. Rust is known for providing the control and performance of languages like C and C++, but with built-in safety features that prevent common programming errors, such as memory leaks, data races, and buffer overflows.", uuid::Uuid::new_v4()),+ ("Rust as a Natural Process (Oxidation) refers to the chemical reaction that occurs when metals, primarily iron, come into contact with oxygen and moisture (water) over time, leading to the formation of iron oxide, commonly known as rust. This process is a form of oxidation, where a substance reacts with oxygen in the air or water, resulting in the degradation of the metal.", uuid::Uuid::new_v4())] {+ ids.push(id.to_string());+ chat.embed_paragraphs(&id.to_string(), vec![doc.to_string()]).await.unwrap();+ }+ chat.set_rag_ids(ids.clone()).await.unwrap();++ let stream = chat+ .stream_question("Rust is a multiplayer survival game", Default::default())+ .await+ .unwrap();+ let (answer, sources) = collect_stream(stream).await;+ dbg!(&answer);+ dbg!(&sources);++ assert!(!answer.is_empty());+ assert_eq!(sources.len(), 1);+}++#[tokio::test]+async fn local_ollama_test_chat_format() {+ let context = TestContext::new().unwrap();+ let mut chat = context.create_chat(vec![]).await;+ let mut format = ResponseFormat::new();+ format.output_layout = OutputLayout::SimpleTable;++ let stream = chat+ .stream_question("Compare rust and js", format)+ .await+ .unwrap();+ let (answer, _) = collect_stream(stream).await;+ dbg!(&answer);+ assert!(!answer.is_empty());+}++async fn collect_stream(stream: StreamAnswer) -> (String, Vec<String>) {+ let mut result = String::new();+ let mut sources = vec![];+ let mut stream = stream;+ while let Some(chunk) = stream.next().await {+ match chunk {+ Ok(value) => match value {+ QuestionStreamValue::Answer { value } => {+ result.push_str(&value);+ },+ QuestionStreamValue::Metadata { value } => {+ sources.push(value.get(SOURCE_ID).unwrap().to_string());+ },+ QuestionStreamValue::KeepAlive => {},+ },+ Err(e) => {+ eprintln!("Error: {}", e);+ },+ }+ }++ (result, sources)+}++#[tokio::test]+async fn local_ollama_test_chat_related_question() {+ setup_log();++ let ollama = LLMOllama::default().with_model("llama3.1");+ let chain = RelatedQuestionChain::new(ollama);+ let resp = chain+ .related_question("Compare rust with JS")+ .await+ .unwrap();++ dbg!(&resp);+ assert_eq!(resp.len(), 3);+}diff --git a/frontend/rust-lib/flowy-ai/tests/complete_test/mod.rs b/frontend/rust-lib/flowy-ai/tests/complete_test/mod.rsnew file mode 100644index 0000000000..872b00d0dc--- /dev/null+++ b/frontend/rust-lib/flowy-ai/tests/complete_test/mod.rs@@ -0,0 +1,815 @@+use crate::setup_log;+use flowy_ai::local_ai::chat::llm::LLMOllama;+use flowy_ai::local_ai::completion::chain::CompletionChain;+use flowy_ai::local_ai::completion::stream_interpreter::stream_interpreter_for_completion;+use flowy_ai_pub::cloud::{CompletionStreamValue, CompletionType, OutputLayout, ResponseFormat};+use flowy_error::FlowyError;+use futures_util::{stream, stream::BoxStream, StreamExt};+use langchain_rust::language_models::LLMError;+use langchain_rust::schemas::StreamData;+use serde_json::json;+use tokio_stream::wrappers::ReceiverStream;+use tracing::error;++#[tokio::test]+async fn local_ollama_test_simple_ask_ai() {+ let ollama = LLMOllama::default().with_model("llama3.1");+ let ai_completion = CompletionChain::new(ollama);+ let text = "Compare js with Rust";+ let ty = CompletionType::AskAI;+ let mut format = ResponseFormat::new();+ format.output_layout = OutputLayout::SimpleTable;++ let stream = ai_completion+ .complete(text, ty, format, None)+ .await+ .unwrap();+ let (answer, comment) = collect_completion_stream(stream).await;+ dbg!(&answer);+ assert!(!answer.is_empty());+ assert!(comment.is_empty());+}++#[tokio::test]+async fn local_ollama_test_improve_writing() {+ setup_log();+ let ollama = LLMOllama::default().with_model("llama3.1");+ let ai_completion = CompletionChain::new(ollama);+ let text = "I like playing basketball with my friend";+ let ty = CompletionType::ImproveWriting;+ let format = ResponseFormat::default();+ let stream = ai_completion+ .complete(text, ty, format, None)+ .await+ .unwrap();+ let (answer, comment) = collect_completion_stream(stream).await;+ dbg!(&answer);+ dbg!(&comment);+ assert!(!answer.is_empty());+ assert!(!comment.is_empty());+}++#[tokio::test]+async fn local_ollama_test_simple_fix_grammar() {+ setup_log();+ let ollama = LLMOllama::default().with_model("llama3.1");+ let ai_completion = CompletionChain::new(ollama);+ let text = "He starts work everyday at 8 a.m";+ let ty = CompletionType::SpellingAndGrammar;+ let format = ResponseFormat::default();+ let stream = ai_completion+ .complete(text, ty, format, None)+ .await+ .unwrap();+ let (answer, comment) = collect_completion_stream(stream).await;+ dbg!(&answer);+ dbg!(&comment);+ assert!(!answer.is_empty());+ assert!(!comment.is_empty());+}++async fn collect_completion_stream(+ mut stream: ReceiverStream<Result<CompletionStreamValue, FlowyError>>,+) -> (String, String) {+ let mut answer = Vec::new();+ let mut comment = Vec::new();+ while let Some(item) = stream.next().await {+ match item {+ Ok(CompletionStreamValue::Answer { value }) => {+ dbg!(&value);+ answer.push(value);+ },+ Ok(CompletionStreamValue::Comment { value }) => {+ dbg!(&value);+ comment.push(value);+ },+ Err(e) => {+ error!("[collect_completion_stream] error: {:?}", e);+ },+ }+ }+ (answer.join(""), comment.join(""))+}++/// Helper function to create a mock stream from chunks of text+pub fn create_mock_stream(chunks: Vec<&str>) -> BoxStream<'static, Result<StreamData, LLMError>> {+ setup_log();+ let stream_items = chunks+ .into_iter()+ .map(|chunk| {+ Ok(StreamData {+ value: json!({+ "message": {+ "content": chunk+ }+ }),+ content: chunk.to_string(),+ tokens: None,+ })+ })+ .collect::<Vec<Result<StreamData, LLMError>>>();++ stream::iter(stream_items).boxed()+}++/// Collects all values from a completion stream into a tuple of (answer, comment)+pub async fn collect_completion_results(+ mut stream: BoxStream<'static, Result<CompletionStreamValue, LLMError>>,+) -> (String, String) {+ let mut answer = String::new();+ let mut comment = String::new();++ while let Some(result) = stream.next().await {+ match result {+ Ok(CompletionStreamValue::Answer { value }) => {+ answer.push_str(&value);+ },+ Ok(CompletionStreamValue::Comment { value }) => {+ comment.push_str(&value);+ },+ _ => {},+ }+ }++ (answer, comment)+}++#[tokio::test]+async fn test_multiple_tags_same_type() {+ // Test when multiple tags of the same type appear in the stream+ let chunks = vec![+ "**Improved**\nFirst improvement.\n\n",+ "**Explanation**\nThe explanation.",+ ];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::ImproveWriting);++ let (answer, comment) = collect_completion_results(result_stream).await;++ // It should use the first "Improved" tag's content+ assert_eq!(answer, "First improvement.");+ assert_eq!(comment, "The explanation.");+}++// #[tokio::test]+// async fn test_empty_content_between_tags() {+// // Test when there's no content between tags+// let chunks = vec!["**Improved**\n\n", "**Explanation**\nSome explanation."];+//+// let stream = create_mock_stream(chunks);+// let result_stream = stream_interpreter_for_completion(stream, CompletionType::ImproveWriting);+//+// let (answer, comment) = collect_completion_results(result_stream).await;+//+// assert_eq!(answer, ""); // Empty content should result in empty answer+// assert_eq!(comment, "Some explanation.");+// }++#[tokio::test]+async fn test_case_insensitive_tags() {+ // Test tags with different cases+ let chunks = vec![+ "**improved**\nLowercase tag.\n\n",+ "**EXPLANATION**\nUppercase tag.",+ ];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::ImproveWriting);++ let (answer, comment) = collect_completion_results(result_stream).await;++ assert_eq!(answer, "Lowercase tag.");+ assert_eq!(comment, "Uppercase tag.");+}++#[tokio::test]+async fn test_xml_style_tags() {+ // Test XML-style tags as an alternative format+ let chunks = vec![+ "<Improved>\nXML style improved content.\n</Improved>\n",+ "<Explanation>\nXML style explanation.\n</Explanation>",+ ];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::ImproveWriting);++ let (answer, comment) = collect_completion_results(result_stream).await;++ assert_eq!(answer, "XML style improved content.");+ assert_eq!(comment, "XML style explanation.");+}++#[tokio::test]+async fn test_malformed_tags() {+ // Test incomplete or malformed tags+ let chunks = vec![+ "**Improved\nMalformed opening tag.\n\n", // Missing closing **+ "Explanation**\nMalformed opening tag again.\n\n", // Missing opening **+ "**Improved**\nThis should be recognized.\n\n",+ "**Explanation**\nProper explanation.",+ ];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::ImproveWriting);++ let (answer, comment) = collect_completion_results(result_stream).await;++ // The parser should ignore malformed tags and find the proper ones+ assert_eq!(answer, "This should be recognized.");+ assert_eq!(comment, "Proper explanation.");+}++#[tokio::test]+async fn test_large_content() {+ // Test with larger chunks of content+ let large_text = "A ".repeat(1000) + "large text";+ let large_explanation = "The ".repeat(1000) + "explanation";++ let chunks = [+ format!("**Improved**\n{}\n\n", large_text),+ format!("**Explanation**\n{}", large_explanation),+ ];++ let stream = create_mock_stream(chunks.iter().map(|s| s.as_str()).collect());+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::ImproveWriting);++ let (answer, comment) = collect_completion_results(result_stream).await;++ assert_eq!(answer, large_text);+ assert_eq!(comment, large_explanation);+}++#[tokio::test]+async fn test_no_tags() {+ // Test when no tags are present in the response+ let chunks = vec![+ "This content has no tags.",+ "It should be passed through as is.",+ ];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::ImproveWriting);++ let (answer, comment) = collect_completion_results(result_stream).await;++ // Without tags, we shouldn't extract any content+ assert_eq!(answer, "");+ assert_eq!(comment, "");+}++#[tokio::test]+async fn test_spelling_grammar_completion_type() {+ // Test with SpellingAndGrammar completion type+ let chunks = vec![+ "**Corrected**\nCorrected spelling.\n\n",+ "**Explanation**\nSpelling corrections explanation.",+ ];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::SpellingAndGrammar);++ let (answer, comment) = collect_completion_results(result_stream).await;++ assert_eq!(answer, "Corrected spelling.");+ assert_eq!(comment, "Spelling corrections explanation.");+}++#[tokio::test]+async fn test_delayed_tag_detection() {+ // Test when there's a lot of content before any tag appears+ let prefix = "This is a lot of preliminary text that contains no tags.\n".repeat(10);++ let chunks = vec![+ &prefix,+ "**Improved**\nFinally, the improved content.\n\n",+ "**Explanation**\nThe explanation after delay.",+ ];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::ImproveWriting);+ let (answer, comment) = collect_completion_results(result_stream).await;++ assert_eq!(answer, "Finally, the improved content.");+ assert_eq!(comment, "The explanation after delay.");+}++// #[tokio::test]+// async fn test_non_standard_tag_formats() {+// // Test with tag format variations that might be produced by different LLMs+// let chunks = vec![+// "**[Improved]**\nBracketed tag format.\n\n",+// "**<Explanation>**\nAngled bracket in markdown.",+// ];+//+// let stream = create_mock_stream(chunks);+// let result_stream = stream_interpreter_for_completion(stream, CompletionType::ImproveWriting);+//+// let (answer, comment) = collect_completion_results(result_stream).await;+//+// // These tags don't match our standard format, so should be ignored+// assert_eq!(answer, "");+// assert_eq!(comment, "");+// }++#[tokio::test]+async fn test_multiple_completion_calls() {+ // Test processing multiple completion rounds in sequence+ // First round+ let chunks1 = vec![+ "**Improved**\nFirst round improvement.\n\n",+ "**Explanation**\nFirst explanation.",+ ];++ let stream1 = create_mock_stream(chunks1);+ let result_stream1 = stream_interpreter_for_completion(stream1, CompletionType::ImproveWriting);+ let (answer1, comment1) = collect_completion_results(result_stream1).await;++ // Second round with different content+ let chunks2 = vec![+ "**Improved**\nSecond round improvement.\n\n",+ "**Explanation**\nSecond explanation.",+ ];++ let stream2 = create_mock_stream(chunks2);+ let result_stream2 = stream_interpreter_for_completion(stream2, CompletionType::ImproveWriting);+ let (answer2, comment2) = collect_completion_results(result_stream2).await;++ // Each round should be processed independently+ assert_eq!(answer1, "First round improvement.");+ assert_eq!(comment1, "First explanation.");+ assert_eq!(answer2, "Second round improvement.");+ assert_eq!(comment2, "Second explanation.");+}++#[tokio::test]+async fn test_special_characters_in_content() {+ // Test content with special characters that might affect parsing+ let special_content =+ "**Improved**\nContent with **asterisks**, <brackets>, \n\nand other $p3c!@l characters.\n\n";+ let special_explanation =+ "**Explanation**\nExplanation with **formatting** and <xml-like> elements.";++ let chunks = vec![special_content, special_explanation];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::ImproveWriting);++ let (answer, comment) = collect_completion_results(result_stream).await;++ assert_eq!(+ answer,+ "Content with **asterisks**, <brackets>, \n\nand other $p3c!@l characters."+ );+ assert_eq!(+ comment,+ "Explanation with **formatting** and <xml-like> elements."+ );+}++#[tokio::test]+async fn test_content_with_unicode() {+ // Test content with unicode characters+ let unicode_content = "**Improved**\n🚀 Unicode content with emoji 😊 and international text: こんにちは, Привет, مرحبا\n\n";+ let unicode_explanation = "**Explanation**\n🌎 Unicode explanation: 안녕하세요, Olá, 你好";++ let chunks = vec![unicode_content, unicode_explanation];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::ImproveWriting);++ let (answer, comment) = collect_completion_results(result_stream).await;++ assert_eq!(+ answer,+ "🚀 Unicode content with emoji 😊 and international text: こんにちは, Привет, مرحبا"+ );+ assert_eq!(comment, "🌎 Unicode explanation: 안녕하세요, Olá, 你好");+}++#[tokio::test]+async fn test_empty_stream() {+ // Test with an empty stream+ let chunks: Vec<&str> = vec![];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::ImproveWriting);++ let (answer, comment) = collect_completion_results(result_stream).await;++ assert_eq!(answer, "");+ assert_eq!(comment, "");+}++#[tokio::test]+async fn test_tiny_chunks_with_mixed_tag_formats() {+ // Test when content is delivered in very small chunks with mixed tag formats+ let chunks = vec![+ "**",+ "Correct",+ "ed",+ "**\n",+ "He",+ " starts",+ " work",+ " every",+ " day",+ " at",+ " **",+ "8",+ ":",+ "00",+ " a",+ ".m",+ ".",+ "**", // not end with </Corrected>+ "\n\n",+ "<Explanation",+ ">\n",+ "*",+ " \"",+ "every",+ "day",+ "\"",+ " should",+ " be",+ " changed",+ " to",+ " \"",+ "every",+ " day",+ "\"",+ " as",+ " it",+ " is",+ " an",+ " ad",+ "verb",+ " and",+ " should",+ " not",+ " be",+ " written",+ " with",+ " the",+ " suffix",+ " \"-",+ "day",+ "\".\n",+ "*",+ " Added",+ " colon",+ " after",+ " the",+ " time",+ " for",+ " correct",+ " formatting",+ ".\n",+ "*",+ " Added",+ " zeros",+ " for",+ " clarity",+ " in",+ " time",+ " notation",+ ".\n",+ "</",+ "Explanation",+ ">",+ "\n",+ ];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::SpellingAndGrammar);++ let (answer, comment) = collect_completion_results(result_stream).await;++ assert_eq!(answer, "He starts work every day at **8:00 a.m.**");+ assert_eq!(comment, "* \"everyday\" should be changed to \"every day\" as it is an adverb and should not be written with the suffix \"-day\".\n* Added colon after the time for correct formatting.\n* Added zeros for clarity in time notation.");+}++#[tokio::test]+async fn test_format_tags_with_grammar_correction() {+ // Test when content uses both markdown and XML tag formats+ let chunks = vec![+ "<Correct",+ "ed",+ ">",+ "He",+ " starts",+ " work",+ " every",+ " day",+ " at",+ " ",+ "8",+ " a",+ ".m",+ ".</",+ "Correct",+ "ed",+ ">\n",+ "<Explanation",+ ">\n",+ "*",+ " The",+ " word",+ " \"",+ "every",+ "day",+ "\"",+ " is",+ " being",+ " replaced",+ " with",+ " **",+ "`",+ "every",+ " day",+ "`",+ "**,",+ " as",+ " the",+ " first",+ " part",+ " should",+ " be",+ " hy",+ "phen",+ "ated",+ " for",+ " clarity",+ ".",+ " However",+ ",",+ " in",+ " this",+ " case",+ ",",+ " it",+ "'s",+ " more",+ " idi",+ "omatic",+ " to",+ " use",+ " **",+ "`",+ "every",+ " day",+ "`",+ "**",+ " without",+ " a",+ " space",+ ".\n",+ "*",+ " In",+ " British",+ " English",+ ",",+ " it",+ "'s",+ " common",+ " to",+ " write",+ " out",+ " the",+ " time",+ " (",+ "e",+ ".g",+ ".,",+ " \"",+ "eight",+ " o",+ "'clock",+ "\"",+ " or",+ " \"",+ "8",+ " a",+ ".m",+ ".\")",+ ".",+ " Since",+ " the",+ " text",+ " already",+ " uses",+ " \"",+ "8",+ " a",+ ".m",+ ".\",",+ " no",+ " correction",+ " is",+ " needed",+ " here",+ ".",+ ];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::SpellingAndGrammar);++ let (answer, comment) = collect_completion_results(result_stream).await;++ // Assert the expected output - updated to match actual behavior+ assert_eq!(answer, "He starts work every day at 8 a.m.");+ assert_eq!(comment, "* The word \"everyday\" is being replaced with **`every day`**, as the first part should be hyphenated for clarity. However, in this case, it's more idiomatic to use **`every day`** without a space.\n* In British English, it's common to write out the time (e.g., \"eight o'clock\" or \"8 a.m.\"). Since the text already uses \"8 a.m.\", no correction is needed here.");+}++#[tokio::test]+async fn test_everyday_correction_stream() {+ // Test with stream chunks for correcting "everyday" to "every day"+ let chunks = vec![+ "<Correct",+ "ed",+ ">",+ "He",+ " starts",+ " work",+ " every",+ " day",+ " at",+ " ",+ "8",+ " a",+ ".m",+ ".</",+ "Correct",+ "ed",+ ">",+ "\n",+ "<Explanation",+ ">The",+ " error",+ " was",+ " in",+ " \"",+ "every",+ "day",+ "\".",+ " In",+ " English",+ ",",+ " \"",+ "every",+ "day",+ "\"",+ " is",+ " an",+ " adjective",+ " that",+ " means",+ " happening",+ " or",+ " done",+ " regularly",+ ".",+ " The",+ " correct",+ " word",+ " to",+ " use",+ " here",+ " is",+ " \"",+ "every",+ " day",+ "\",",+ " which",+ " is",+ " a",+ " noun",+ " phrase",+ " indicating",+ " a",+ " specific",+ " time",+ " period",+ ".",+ " I",+ " added",+ " a",+ " space",+ " between",+ " the",+ " two",+ " words",+ " for",+ " clarity",+ " and",+ " correctness",+ ".</",+ "Explanation",+ ">",+ "\n",+ ];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::SpellingAndGrammar);++ let (answer, comment) = collect_completion_results(result_stream).await;++ assert_eq!(answer, "He starts work every day at 8 a.m.");+ assert_eq!(comment, "The error was in \"everyday\". In English, \"everyday\" is an adjective that means happening or done regularly. The correct word to use here is \"every day\", which is a noun phrase indicating a specific time period. I added a space between the two words for clarity and correctness.");+}++#[tokio::test]+async fn test_basketball_improvement_stream() {+ // Test with stream chunks for improving a basketball-related sentence+ let chunks = vec![+ "**",+ "Improved",+ "**\n",+ "We",+ " enjoy",+ " playing",+ " basketball",+ " together",+ " as",+ " friends",+ ".\n\n",+ "<Explanation",+ ">\n",+ "*",+ " Changed",+ " \"",+ "like",+ "\"",+ " to",+ " \"",+ "en",+ "joy",+ "\",",+ " which",+ " is",+ " a",+ " more",+ " specific",+ " and",+ " engaging",+ " verb",+ " that",+ " con",+ "veys",+ " a",+ " stronger",+ " enthusiasm",+ " for",+ " the",+ " activity",+ ".\n",+ "*",+ " Modified",+ " the",+ " sentence",+ " structure",+ " to",+ " make",+ " it",+ " more",+ " concise",+ " and",+ " natural",+ "-s",+ "ounding",+ ",",+ " using",+ " a",+ " more",+ " active",+ " voice",+ " (\"",+ "We",+ " enjoy",+ "...",+ "\")",+ " instead",+ " of",+ " a",+ " passive",+ " one",+ " (\"",+ "I",+ " like",+ "...",+ "\").",+ "</",+ "Explanation",+ ">",+ "\n",+ ];++ let stream = create_mock_stream(chunks);+ let result_stream = stream_interpreter_for_completion(stream, CompletionType::ImproveWriting);++ let (answer, comment) = collect_completion_results(result_stream).await;++ assert_eq!(answer, "We enjoy playing basketball together as friends.");+ assert_eq!(comment, "* Changed \"like\" to \"enjoy\", which is a more specific and engaging verb that conveys a stronger enthusiasm for the activity.\n* Modified the sentence structure to make it more concise and natural-sounding, using a more active voice (\"We enjoy...\") instead of a passive one (\"I like...\").");+}diff --git a/frontend/rust-lib/flowy-ai/tests/main.rs b/frontend/rust-lib/flowy-ai/tests/main.rsnew file mode 100644index 0000000000..00f7ad2d3e--- /dev/null+++ b/frontend/rust-lib/flowy-ai/tests/main.rs@@ -0,0 +1,25 @@+mod chat_test;+mod complete_test;+mod summary_test;+mod translate_test;++use std::sync::Once;+use tracing_subscriber::fmt::Subscriber;+use tracing_subscriber::util::SubscriberInitExt;+use tracing_subscriber::EnvFilter;++pub fn setup_log() {+ static START: Once = Once::new();+ START.call_once(|| {+ let level = std::env::var("RUST_LOG").unwrap_or("trace".to_string());+ let mut filters = vec![];+ filters.push(format!("flowy_ai={}", level));+ std::env::set_var("RUST_LOG", filters.join(","));++ let subscriber = Subscriber::builder()+ .with_ansi(true)+ .with_env_filter(EnvFilter::from_default_env())+ .finish();+ subscriber.try_init().unwrap();+ });+}diff --git a/frontend/rust-lib/flowy-ai/tests/summary_test/mod.rs b/frontend/rust-lib/flowy-ai/tests/summary_test/mod.rsnew file mode 100644index 0000000000..362e6c9b0b--- /dev/null+++ b/frontend/rust-lib/flowy-ai/tests/summary_test/mod.rs@@ -0,0 +1,30 @@+use crate::setup_log;+use flowy_ai::local_ai::chat::llm::LLMOllama;+use flowy_ai::local_ai::database::summary::DatabaseSummaryChain;+use std::collections::HashMap;++#[tokio::test]+async fn local_ollama_test_database_summary() {+ setup_log();++ let ollama = LLMOllama::default().with_model("llama3.1");+ let chain = DatabaseSummaryChain::new(ollama);++ let mut data = HashMap::new();+ data.insert("book name".to_string(), "Atomic Habits".to_string());+ data.insert("finish reading at".to_string(), "2023-02-10".to_string());+ data.insert(+ "notes".to_string(),+ "An atomic habit is a regular practice or routine that is not \+only small and easy to do but is also the source of incredible power; a \+component of the system of compound growth. Bad habits repeat themselves \+again and again not because you don't want to change, but because you \+have the wrong system for change. Changes that seem small and \+unimportant at first will compound into remarkable results if you're \+willing to stick with them for years"+ .to_string(),+ );++ let resp = chain.summarize(data).await.unwrap();+ dbg!(&resp);+}diff --git a/frontend/rust-lib/flowy-ai/tests/translate_test/mod.rs b/frontend/rust-lib/flowy-ai/tests/translate_test/mod.rsnew file mode 100644index 0000000000..a8acce25ed--- /dev/null+++ b/frontend/rust-lib/flowy-ai/tests/translate_test/mod.rs@@ -0,0 +1,35 @@+use crate::setup_log;+use flowy_ai::local_ai::chat::llm::LLMOllama;+use flowy_ai::local_ai::database::translate::DatabaseTranslateChain;+use flowy_ai_pub::cloud::ai_dto::TranslateRowData;+use flowy_database_pub::cloud::TranslateItem;++#[tokio::test]+async fn local_ollama_test_database_translate() {+ setup_log();++ let ollama = LLMOllama::default().with_model("llama3.1");+ let chain = DatabaseTranslateChain::new(ollama);++ let data = TranslateRowData {+ cells: vec![+ TranslateItem {+ title: "name".to_string(),+ content: "Jask".to_string(),+ },+ TranslateItem {+ title: "age".to_string(),+ content: "25".to_string(),+ },+ TranslateItem {+ title: "city".to_string(),+ content: "New York".to_string(),+ },+ ],+ language: "french".to_string(),+ include_header: false,+ };+ let resp = chain.translate(data).await.unwrap();+ dbg!(&resp);+ assert!(!resp.items.is_empty());+}diff --git a/frontend/rust-lib/flowy-core/src/deps_resolve/cloud_service_impl.rs b/frontend/rust-lib/flowy-core/src/deps_resolve/cloud_service_impl.rsindex c05aa3152a..6aa61e59f4 100644--- a/frontend/rust-lib/flowy-core/src/deps_resolve/cloud_service_impl.rs+++ b/frontend/rust-lib/flowy-core/src/deps_resolve/cloud_service_impl.rs@@ -729,7 +729,7 @@ impl ChatCloudService for ServerProvider {workspace_id: &Uuid,chat_id: &Uuid,message_id: i64,- ai_model: Option<AIModel>,+ ai_model: AIModel,) -> Result<RepeatedRelatedQuestion, FlowyError> {self.get_server()?@@ -755,7 +755,7 @@ impl ChatCloudService for ServerProvider {&self,workspace_id: &Uuid,params: CompleteTextParams,- ai_model: Option<AIModel>,+ ai_model: AIModel,) -> Result<StreamComplete, FlowyError> {let server = self.get_server()?;serverdiff --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 dd68dcdd49..536fb75965 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@@ -1,4 +1,3 @@-use af_local_ai::ai_ops::{LocalAITranslateItem, LocalAITranslateRowData};use collab_integrate::collab_builder::AppFlowyCollabBuilder;use collab_integrate::CollabKVDB;use flowy_ai::ai_manager::AIManager;@@ -50,53 +49,54 @@ impl DatabaseAIService for DatabaseAIServiceMiddleware {&self,workspace_id: &Uuid,object_id: &Uuid,- _summary_row: SummaryRowContent,+ summary_row: SummaryRowContent,) -> Result<String, FlowyError> {- if self.ai_manager.local_ai.is_running() {+ if self+ .ai_manager+ .local_ai+ .is_enabled_on_workspace(&workspace_id.to_string())+ {+ let model = self+ .ai_manager+ .get_active_model(&object_id.to_string())+ .await;self.ai_manager.local_ai- .summary_database_row(_summary_row)+ .summarize_database_row(&model.name, summary_row).await- .map_err(|err| FlowyError::local_ai().with_context(err))} else {self.ai_service- .summary_database_row(workspace_id, object_id, _summary_row)+ .summary_database_row(workspace_id, object_id, summary_row).await}}async fn translate_database_row(&self,- _workspace_id: &Uuid,- _translate_row: TranslateRowContent,- _language: &str,+ workspace_id: &Uuid,+ translate_row: TranslateRowContent,+ language: &str,) -> Result<TranslateRowResponse, FlowyError> {- if self.ai_manager.local_ai.is_running() {- let data = LocalAITranslateRowData {- cells: _translate_row- .into_iter()- .map(|row| LocalAITranslateItem {- title: row.title,- content: row.content,- })- .collect(),- language: _language.to_string(),- include_header: false,- };- let resp = self+ if self+ .ai_manager+ .local_ai+ .is_enabled_on_workspace(&workspace_id.to_string())+ {+ let model = self+ .ai_manager+ .get_active_model(&workspace_id.to_string())+ .await;+ self.ai_manager.local_ai- .translate_database_row(data)+ .translate_database_row(&model.name, translate_row, language).await- .map_err(|err| FlowyError::local_ai().with_context(err))?;-- Ok(TranslateRowResponse { items: resp.items })} else {self.ai_service- .translate_database_row(_workspace_id, _translate_row, _language)+ .translate_database_row(workspace_id, translate_row, language).await}}diff --git a/frontend/rust-lib/flowy-core/src/server_layer.rs b/frontend/rust-lib/flowy-core/src/server_layer.rsindex de45556b85..f23d504c56 100644--- a/frontend/rust-lib/flowy-core/src/server_layer.rs+++ b/frontend/rust-lib/flowy-core/src/server_layer.rs@@ -1,5 +1,4 @@use crate::AppFlowyCoreConfig;-use af_plugin::manager::PluginManager;use arc_swap::{ArcSwap, ArcSwapOption};use dashmap::mapref::one::Ref;use dashmap::DashMap;@@ -60,12 +59,7 @@ impl ServerProvider {let auth_type = ArcSwap::from(Arc::new(initial_auth));let encryption = Arc::new(EncryptionImpl::new(None)) as Arc<dyn AppFlowyEncryption>;let ai_user = Arc::new(AIUserServiceImpl(Arc::downgrade(&logged_user)));- let plugins = Arc::new(PluginManager::new());- let local_ai = Arc::new(LocalAIController::new(- plugins,- store_preferences,- ai_user.clone(),- ));+ let local_ai = Arc::new(LocalAIController::new(store_preferences, ai_user.clone()));ServerProvider {config,diff --git a/frontend/rust-lib/flowy-error/Cargo.toml b/frontend/rust-lib/flowy-error/Cargo.tomlindex 8bc67ee46c..de57c67e3f 100644--- a/frontend/rust-lib/flowy-error/Cargo.toml+++ b/frontend/rust-lib/flowy-error/Cargo.toml@@ -37,7 +37,7 @@ tantivy = { workspace = true, optional = true }uuid.workspace = true[target.'cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))'.dependencies]-ollama-rs = "0.3.0"+ollama-rs.workspace = true[features]diff --git a/frontend/rust-lib/flowy-server/src/af_cloud/impls/chat.rs b/frontend/rust-lib/flowy-server/src/af_cloud/impls/chat.rsindex 4e9b321a84..89355e2ee4 100644--- a/frontend/rust-lib/flowy-server/src/af_cloud/impls/chat.rs+++ b/frontend/rust-lib/flowy-server/src/af_cloud/impls/chat.rs@@ -182,7 +182,7 @@ whereworkspace_id: &Uuid,chat_id: &Uuid,message_id: i64,- ai_model: Option<AIModel>,+ ai_model: AIModel,) -> Result<RepeatedRelatedQuestion, FlowyError> {let try_get_client = self.inner.try_get_client();let resp = try_get_client?@@ -197,12 +197,12 @@ where&self,workspace_id: &Uuid,params: CompleteTextParams,- ai_model: Option<AIModel>,+ ai_model: AIModel,) -> Result<StreamComplete, FlowyError> {let stream = self.inner.try_get_client()?- .stream_completion_v2(workspace_id, params, ai_model.map(|v| v.name))+ .stream_completion_v2(workspace_id, params, Some(ai_model.name)).await.map_err(FlowyError::from)?.map_err(FlowyError::from);diff --git a/frontend/rust-lib/flowy-server/src/local_server/impls/chat.rs b/frontend/rust-lib/flowy-server/src/local_server/impls/chat.rsindex 098de5f8c0..8476799be9 100644--- a/frontend/rust-lib/flowy-server/src/local_server/impls/chat.rs+++ b/frontend/rust-lib/flowy-server/src/local_server/impls/chat.rs@@ -1,14 +1,12 @@use crate::af_cloud::define::LoggedUser;use chrono::{TimeZone, Utc};use client_api::entity::ai_dto::RepeatedRelatedQuestion;-use client_api::entity::CompletionStream;use flowy_ai::local_ai::controller::LocalAIController;-use flowy_ai::local_ai::stream_util::QuestionStream;use flowy_ai_pub::cloud::chat_dto::{ChatAuthor, ChatAuthorType};use flowy_ai_pub::cloud::{- AIModel, AppErrorCode, AppResponseError, ChatCloudService, ChatMessage, ChatMessageType,- ChatSettings, CompleteTextParams, MessageCursor, ModelList, RelatedQuestion, RepeatedChatMessage,- ResponseFormat, StreamAnswer, StreamComplete, UpdateChatParams, DEFAULT_AI_MODEL_NAME,+ AIModel, ChatCloudService, ChatMessage, ChatMessageType, ChatSettings, CompleteTextParams,+ MessageCursor, ModelList, RelatedQuestion, RepeatedChatMessage, ResponseFormat, StreamAnswer,+ StreamComplete, UpdateChatParams, DEFAULT_AI_MODEL_NAME,};use flowy_ai_pub::persistence::{deserialize_chat_metadata, deserialize_rag_ids, read_chat,@@ -17,7 +15,6 @@ use flowy_ai_pub::persistence::{ChatMessageTable, ChatTable, ChatTableChangeset,};use flowy_error::{FlowyError, FlowyResult};-use futures_util::{stream, StreamExt, TryStreamExt};use lib_infra::async_trait::async_trait;use lib_infra::util::timestamp;use serde_json::{json, Value};@@ -109,25 +106,12 @@ impl ChatCloudService for LocalChatServiceImpl {format: ResponseFormat,_ai_model: Option<AIModel>,) -> Result<StreamAnswer, FlowyError> {- if self.local_ai.is_running() {+ if self.local_ai.is_ready().await {let content = self.get_message_content(message_id)?;- match self+ self.local_ai- .stream_question(- &chat_id.to_string(),- &content,- Some(json!(format)),- json!({}),- )+ .stream_question(chat_id, &content, format).await- {- Ok(stream) => Ok(QuestionStream::new(stream).boxed()),- Err(err) => Ok(- stream::once(async { Err(FlowyError::local_ai_unavailable().with_context(err)) }).boxed(),- ),- }- } else if self.local_ai.is_enabled() {- Err(FlowyError::local_ai_not_ready())} else {Err(FlowyError::local_ai_disabled())}@@ -193,12 +177,12 @@ impl ChatCloudService for LocalChatServiceImpl {_workspace_id: &Uuid,chat_id: &Uuid,message_id: i64,- _ai_model: Option<AIModel>,+ ai_model: AIModel,) -> Result<RepeatedRelatedQuestion, FlowyError> {- if self.local_ai.is_running() {+ if self.local_ai.is_ready().await {let questions = self.local_ai- .get_related_question(&chat_id.to_string())+ .get_related_question(&ai_model.name, chat_id, message_id).await.map_err(|err| FlowyError::local_ai().with_context(err))?;trace!("LocalAI related questions: {:?}", questions);@@ -224,30 +208,10 @@ impl ChatCloudService for LocalChatServiceImpl {&self,_workspace_id: &Uuid,params: CompleteTextParams,- _ai_model: Option<AIModel>,+ ai_model: AIModel,) -> Result<StreamComplete, FlowyError> {- if self.local_ai.is_running() {- match self- .local_ai- .complete_text_v2(- ¶ms.text,- params.completion_type.unwrap() as u8,- Some(json!(params.format)),- Some(json!(params.metadata)),- )- .await- {- Ok(stream) => Ok(- CompletionStream::new(- stream.map_err(|err| AppResponseError::new(AppErrorCode::Internal, err.to_string())),- )- .map_err(FlowyError::from)- .boxed(),- ),- Err(_) => Ok(stream::once(async { Err(FlowyError::local_ai_unavailable()) }).boxed()),- }- } else if self.local_ai.is_enabled() {- Err(FlowyError::local_ai_not_ready())+ if self.local_ai.is_ready().await {+ self.local_ai.complete_text(&ai_model.name, params).await} else {Err(FlowyError::local_ai_disabled())}@@ -260,10 +224,10 @@ impl ChatCloudService for LocalChatServiceImpl {chat_id: &Uuid,metadata: Option<HashMap<String, Value>>,) -> Result<(), FlowyError> {- if self.local_ai.is_running() {+ if self.local_ai.is_ready().await {self.local_ai- .embed_file(&chat_id.to_string(), file_path.to_path_buf(), metadata)+ .embed_file(chat_id, file_path.to_path_buf(), metadata).await.map_err(|err| FlowyError::local_ai().with_context(err))?;Ok(())diff --git a/frontend/rust-lib/flowy-sqlite-vec/Cargo.toml b/frontend/rust-lib/flowy-sqlite-vec/Cargo.tomlindex 58406eb047..fe775a6a12 100644--- a/frontend/rust-lib/flowy-sqlite-vec/Cargo.toml+++ b/frontend/rust-lib/flowy-sqlite-vec/Cargo.toml@@ -15,6 +15,8 @@ zerocopy = "0.9.0-alpha.0"tokio.workspace = trueflowy-ai-pub = { workspace = true }serde_json.workspace = true+r2d2 = { version = "0.8" }+r2d2_sqlite = { version = "0.28" }chrono.workspace = true[dev-dependencies]diff --git a/frontend/rust-lib/flowy-sqlite-vec/src/db.rs b/frontend/rust-lib/flowy-sqlite-vec/src/db.rsindex 6efd2800e3..2a94f8c8b8 100644--- a/frontend/rust-lib/flowy-sqlite-vec/src/db.rs+++ b/frontend/rust-lib/flowy-sqlite-vec/src/db.rs@@ -3,28 +3,45 @@ use crate::init_sqlite_vector_extension;use crate::migration::init_sqlite_with_migrations;use anyhow::{Context, Result};use flowy_ai_pub::entities::{EmbeddedChunk, SearchResult};-use rusqlite::{params, Connection, ToSql};+use r2d2::Pool;+use r2d2_sqlite::SqliteConnectionManager;+use rusqlite::{params, ToSql};use serde_json::Value;use std::collections::{HashMap, HashSet};use std::path::PathBuf;-use std::sync::Arc;-use tokio::sync::Mutex;use uuid::Uuid;use zerocopy::IntoBytes;pub struct VectorSqliteDB {- conn: Arc<Mutex<Connection>>,+ pool: Pool<SqliteConnectionManager>,}-unsafe impl Send for VectorSqliteDB {}-unsafe impl Sync for VectorSqliteDB {}-impl VectorSqliteDB {pub fn new(root: PathBuf) -> Result<Self> {- init_sqlite_vector_extension();let db_path = root.join("vector.db");- let conn = Arc::new(Mutex::new(init_sqlite_with_migrations(db_path.as_path())?));- Ok(Self { conn })++ // Setup the connection manager with the database path+ let manager = SqliteConnectionManager::file(db_path);++ // Initialize SQLite extensions and settings in each new connection+ let manager = manager.with_init(|_| {+ init_sqlite_vector_extension();+ Ok(())+ });++ // Create the connection pool+ let pool = Pool::builder()+ .max_size(10) // Adjust based on your needs+ .build(manager)+ .context("Failed to create connection pool")?;++ // Ensure database is migrated+ let mut conn = pool+ .get()+ .context("Failed to get connection for migration")?;+ init_sqlite_with_migrations(&mut conn)?;++ Ok(Self { pool })}pub async fn select_collabs_fragment_ids(@@ -45,7 +62,10 @@ impl VectorSqliteDB {placeholders);- let conn = self.conn.lock().await;+ let conn = self+ .pool+ .get()+ .context("Failed to get connection from pool")?;let mut stmt = conn.prepare(&sql).context("Preparing select_collabs_fragment_ids")?;@@ -68,7 +88,10 @@ impl VectorSqliteDB {}pub async fn delete_collab(&self, workspace_id: &str, object_id: &str) -> Result<()> {- let mut conn = self.conn.lock().await;+ let mut conn = self+ .pool+ .get()+ .context("Failed to get connection from pool")?;let tx = conn.transaction().context("Starting delete_collab transaction")?;@@ -97,7 +120,10 @@ impl VectorSqliteDB {return Ok(());}- let mut conn = self.conn.lock().await;+ let mut conn = self+ .pool+ .get()+ .context("Failed to get connection from pool")?;let tx = conn.transaction().context("Starting transaction")?;// 1) Collect new IDs@@ -196,44 +222,68 @@ impl VectorSqliteDB {pub async fn search(&self,workspace_id: &str,+ object_ids: Vec<String>,query: &[f32],top_k: i32,) -> Result<Vec<SearchResult>> {self- .search_with_score(workspace_id, query, top_k, 0.4)+ .search_with_score(workspace_id, object_ids, query, top_k, 0.4).await}pub async fn search_with_score(&self,workspace_id: &str,+ object_ids: Vec<String>,query: &[f32],top_k: i32,min_score: f32,) -> Result<Vec<SearchResult>> {// clamp min_score to [0,1]let min_score = min_score.clamp(0.0, 1.0);+ if object_ids.is_empty() {+ self+ .search_without_object_ids(workspace_id, query, top_k, min_score)+ .await+ } else {+ self+ .search_with_object_ids(workspace_id, object_ids, query, top_k, min_score)+ .await+ }+ }++ async fn search_without_object_ids(+ &self,+ workspace_id: &str,+ query: &[f32],+ top_k: i32,+ min_score: f32,+ ) -> Result<Vec<SearchResult>> {// distance = 1 - score, so we only want distance <= max_distancelet max_distance = 1.0 - min_score;let query_blob = query.as_bytes();- // k-NN MATCH with embedded distance filter, compute score = 1-distance+ let conn = self+ .pool+ .get()+ .context("Failed to get connection from pool")?;++ // k-NN MATCH without object_id filterlet sql = "\- SELECT- object_id,- content,- metadata,- distance,- (1.0 - distance) AS score- FROM af_collab_embeddings- WHERE embedding MATCH ?- AND k = ?- AND workspace_id = ?- AND distance <= ?- ORDER BY distance ASC- ";-- let conn = self.conn.lock().await;+ SELECT+ object_id,+ content,+ metadata,+ distance,+ (1.0 - distance) AS score+ FROM af_collab_embeddings+ WHERE embedding MATCH ?+ AND k = ?+ AND workspace_id = ?+ AND distance <= ?+ ORDER BY distance ASC+ ";+let mut stmt = conn.prepare(sql)?;let mut rows = stmt.query(params![query_blob, // MATCH@@ -242,7 +292,71 @@ impl VectorSqliteDB {max_distance, // only distances ≤ this])?;- let mut out = Vec::new();+ self.process_search_results(&mut rows)+ }++ async fn search_with_object_ids(+ &self,+ workspace_id: &str,+ object_ids: Vec<String>,+ query: &[f32],+ top_k: i32,+ min_score: f32,+ ) -> Result<Vec<SearchResult>> {+ // distance = 1 - score, so we only want distance <= max_distance+ let max_distance = 1.0 - min_score;+ let query_blob = query.as_bytes();++ let conn = self+ .pool+ .get()+ .context("Failed to get connection from pool")?;++ // Create placeholders for the IN clause+ let placeholders = std::iter::repeat("?")+ .take(object_ids.len())+ .collect::<Vec<_>>()+ .join(", ");++ // k-NN MATCH with object_id filter+ let sql = format!(+ "SELECT+ object_id,+ content,+ metadata,+ distance,+ (1.0 - distance) AS score+ FROM af_collab_embeddings+ WHERE embedding MATCH ?+ AND k = ?+ AND workspace_id = ?+ AND object_id IN ({})+ AND distance <= ?+ ORDER BY distance ASC",+ placeholders+ );++ let mut stmt = conn.prepare(&sql)?;++ // Create the parameter vector with all params+ let mut query_params: Vec<&dyn ToSql> = Vec::with_capacity(4 + object_ids.len());+ query_params.push(&query_blob as &dyn ToSql);+ query_params.push(&top_k as &dyn ToSql);+ query_params.push(&workspace_id as &dyn ToSql);++ // Add each object_id as an individual parameter+ for oid in &object_ids {+ query_params.push(oid as &dyn ToSql);+ }++ query_params.push(&max_distance as &dyn ToSql);+ let mut rows = stmt.query(query_params.as_slice())?;+ self.process_search_results(&mut rows)+ }++ /// Process the query results and convert them to SearchResult objects+ fn process_search_results(&self, rows: &mut rusqlite::Rows) -> Result<Vec<SearchResult>> {+ let mut results = Vec::new();while let Some(row) = rows.next()? {let oid_str: String = row.get(0)?;let oid = match Uuid::parse_str(&oid_str) {@@ -255,7 +369,7 @@ impl VectorSqliteDB {.and_then(|s| serde_json::from_str::<Value>(&s).ok());let score: f32 = row.get(4)?;- out.push(SearchResult {+ results.push(SearchResult {oid,content,metadata,@@ -263,7 +377,7 @@ impl VectorSqliteDB {});}- Ok(out)+ Ok(results)}pub async fn delete_pending_indexed_collab(@@ -276,8 +390,11 @@ impl VectorSqliteDB {return Ok(());}- // Lock and begin a transaction- let mut conn = self.conn.lock().await;+ // Get connection from pool+ let mut conn = self+ .pool+ .get()+ .context("Failed to get connection from pool")?;let tx = conn.transaction().context("Starting delete_pending_indexed_collab transaction")?;@@ -321,8 +438,11 @@ impl VectorSqliteDB {return Ok(());}- // lock once, start one transaction- let mut conn = self.conn.lock().await;+ // Get connection from pool+ let mut conn = self+ .pool+ .get()+ .context("Failed to get connection from pool")?;let tx = conn.transaction().context("Starting transaction")?;// prepare INSERT only oncediff --git a/frontend/rust-lib/flowy-sqlite-vec/src/migration.rs b/frontend/rust-lib/flowy-sqlite-vec/src/migration.rsindex 53c0eed57e..84f0c27cc9 100644--- a/frontend/rust-lib/flowy-sqlite-vec/src/migration.rs+++ b/frontend/rust-lib/flowy-sqlite-vec/src/migration.rs@@ -1,7 +1,7 @@use include_dir::{include_dir, Dir};-use rusqlite::Connection;+use r2d2::PooledConnection;+use r2d2_sqlite::SqliteConnectionManager;use rusqlite_migration::Migrations;-use std::path::Path;use std::sync::LazyLock;static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");@@ -9,8 +9,9 @@ static MIGRATIONS: LazyLock<Migrations<'static>> =LazyLock::new(|| Migrations::from_directory(&MIGRATIONS_DIR).unwrap());/// Initialize a new SQLite database with migrations-pub fn init_sqlite_with_migrations(db_path: &Path) -> anyhow::Result<Connection> {- let mut conn = Connection::open(db_path)?;- MIGRATIONS.to_latest(&mut conn)?;- Ok(conn)+pub fn init_sqlite_with_migrations(+ conn: &mut PooledConnection<SqliteConnectionManager>,+) -> anyhow::Result<()> {+ MIGRATIONS.to_latest(conn)?;+ Ok(())}diff --git a/frontend/rust-lib/flowy-sqlite-vec/tests/main.rs b/frontend/rust-lib/flowy-sqlite-vec/tests/main.rsindex ffdd317850..272040064b 100644--- a/frontend/rust-lib/flowy-sqlite-vec/tests/main.rs+++ b/frontend/rust-lib/flowy-sqlite-vec/tests/main.rs@@ -8,9 +8,6 @@ use uuid::Uuid;#[tokio::test]async fn test_vector_sqlite_db_basic_operations() -> Result<()> {- // Initialize sqlite-vec extension- init_sqlite_vector_extension();-// Create a temporary directory for the test databaselet temp_dir = tempdir()?;@@ -39,9 +36,6 @@ async fn test_vector_sqlite_db_basic_operations() -> Result<()> {#[tokio::test]async fn test_upsert_and_remove_fragments() -> Result<()> {- // Initialize sqlite-vec extension- init_sqlite_vector_extension();-// Create a temporary directory for the test databaselet temp_dir = tempdir()?;@@ -79,6 +73,7 @@ async fn test_upsert_and_remove_fragments() -> Result<()> {let result = db.search(&workspace_id.to_string(),+ vec![],&generate_embedding_with_size(768, 0.1),1,)@@ -94,7 +89,6 @@ async fn test_upsert_and_remove_fragments() -> Result<()> {#[tokio::test]async fn test_empty_fragments_noop_and_select_empty() -> Result<()> {- init_sqlite_vector_extension();let temp_dir = tempdir()?;let db = VectorSqliteDB::new(temp_dir.into_path())?;@@ -160,7 +154,6 @@ async fn test_duplicate_upsert_idempotent() -> Result<()> {#[tokio::test]async fn test_search_no_hits() -> Result<()> {- init_sqlite_vector_extension();let temp_dir = tempdir()?;let db = VectorSqliteDB::new(temp_dir.into_path())?;@@ -177,7 +170,7 @@ async fn test_search_no_hits() -> Result<()> {// Query with a very different vector should return emptylet query = generate_embedding_with_size(768, -1.0);- let results = db.search(&workspace_id, &query, 1).await?;+ let results = db.search(&workspace_id, vec![], &query, 1).await?;assert!(results.is_empty(),"Expected no near neighbors for orthogonal vector"@@ -187,7 +180,6 @@ async fn test_search_no_hits() -> Result<()> {#[tokio::test]async fn test_multi_workspace_isolation() -> Result<()> {- init_sqlite_vector_extension();let temp_dir = tempdir()?;let db = VectorSqliteDB::new(temp_dir.into_path())?;@@ -205,14 +197,14 @@ async fn test_multi_workspace_isolation() -> Result<()> {// Searching in ws1 should not return ws2's fragmentlet res1 = db- .search(&ws1, &generate_embedding_with_size(768, 0.9), 1)+ .search(&ws1, vec![], &generate_embedding_with_size(768, 0.9), 1).await?;assert_eq!(res1.len(), 1);assert_eq!(res1[0].oid, Uuid::parse_str(&oid)?);// Searching in ws2 should not return ws1's fragmentlet res2 = db- .search(&ws2, &generate_embedding_with_size(768, -0.9), 1)+ .search(&ws2, vec![], &generate_embedding_with_size(768, -0.9), 1).await?;assert_eq!(res2.len(), 1);assert_eq!(res2[0].oid, Uuid::parse_str(&oid)?);@@ -222,7 +214,6 @@ async fn test_multi_workspace_isolation() -> Result<()> {#[tokio::test]async fn test_select_multiple_oids() -> Result<()> {- init_sqlite_vector_extension();let temp_dir = tempdir()?;let db = VectorSqliteDB::new(temp_dir.into_path())?;@@ -265,7 +256,6 @@ async fn test_select_multiple_oids() -> Result<()> {#[tokio::test]async fn test_skip_missing_content() -> Result<()> {- init_sqlite_vector_extension();let temp_dir = tempdir()?;let db = VectorSqliteDB::new(temp_dir.into_path())?;@@ -288,6 +278,91 @@ async fn test_skip_missing_content() -> Result<()> {Ok(())}+#[tokio::test]+async fn test_object_ids_handling() -> Result<()> {+ let temp_dir = tempdir()?;+ let db = VectorSqliteDB::new(temp_dir.into_path())?;+ let workspace_id = Uuid::new_v4().to_string();++ // Test with different object ID formats+ let standard_oid = Uuid::new_v4().to_string();+ let special_oid = Uuid::new_v4().to_string(); // Zero UUID instead of empty+ let very_long_oid = Uuid::new_v4().to_string();++ // Insert fragments with different oids+ db.upsert_collabs_embeddings(+ &workspace_id,+ &standard_oid,+ vec![create_test_fragment(+ &standard_oid,+ 0,+ generate_embedding_with_size(768, 0.1),+ )],+ )+ .await?;++ db.upsert_collabs_embeddings(+ &workspace_id,+ &special_oid,+ vec![create_test_fragment(+ &special_oid,+ 0,+ generate_embedding_with_size(768, 0.2),+ )],+ )+ .await?;++ db.upsert_collabs_embeddings(+ &workspace_id,+ &very_long_oid,+ vec![create_test_fragment(+ &very_long_oid,+ 0,+ generate_embedding_with_size(768, 0.3),+ )],+ )+ .await?;++ // Verify we can retrieve all three types of object IDs+ let all_oids = [+ standard_oid.clone(),+ special_oid.clone(),+ very_long_oid.clone(),+ ];+ let result = db.select_collabs_fragment_ids(&all_oids).await?;++ assert_eq!(+ result.len(),+ 3,+ "Should retrieve fragments for all three object IDs"+ );+ assert!(result.contains_key(&Uuid::parse_str(&standard_oid)?));+ assert!(result.contains_key(&Uuid::parse_str(&special_oid)?));+ assert!(result.contains_key(&Uuid::parse_str(&very_long_oid)?));++ // Test search functionality works with different object IDs+ let search_results = db+ .search(+ &workspace_id,+ vec![],+ &generate_embedding_with_size(768, 0.2),+ 10,+ )+ .await?;++ // Verify we can find results for our special UUID+ let found_special_id = search_results+ .iter()+ .any(|r| r.content == "Content for fragment 0" && r.oid.to_string() == special_oid);++ assert!(+ found_special_id,+ "Should find the fragment with special UUID"+ );++ Ok(())+}+fn generate_embedding_with_size(size: usize, value: f32) -> Vec<f32> {vec![value; size]}diff --git a/frontend/rust-lib/flowy-user-pub/Cargo.toml b/frontend/rust-lib/flowy-user-pub/Cargo.tomlindex f8a673e918..6d39f163c3 100644--- a/frontend/rust-lib/flowy-user-pub/Cargo.toml+++ b/frontend/rust-lib/flowy-user-pub/Cargo.toml@@ -16,7 +16,7 @@ serde_json.workspace = trueserde_repr.workspace = truechrono = { workspace = true, default-features = false, features = ["clock", "serde"] }tokio = { workspace = true, features = ["sync"] }-tokio-stream = "0.1.14"+tokio-stream.workspace = trueflowy-folder-pub.workspace = truecollab-folder = { workspace = true }tracing.workspace = truediff --git a/frontend/rust-lib/flowy-user/Cargo.toml b/frontend/rust-lib/flowy-user/Cargo.tomlindex 0354ef81c5..9cfe9bbee2 100644--- a/frontend/rust-lib/flowy-user/Cargo.toml+++ b/frontend/rust-lib/flowy-user/Cargo.toml@@ -42,7 +42,7 @@ fancy-regex = "0.11.0"uuid.workspace = truechrono = { workspace = true, default-features = false, features = ["clock"] }base64 = "^0.21"-tokio-stream = "0.1.14"+tokio-stream.workspace = truesemver = "1.0.22"validator = { workspace = true, features = ["derive"] }rayon = "1.10.0"