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: support local chat
Nathanf62686fdeb13
diff --git a/frontend/rust-lib/Cargo.lock b/frontend/rust-lib/Cargo.lockindex 671d8bccf2..342e454b9d 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=873415478ed58686c98df578e2c39d07ddce6773#873415478ed58686c98df578e2c39d07ddce6773"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=39057f85a408a40dc702a9e54fd1b9dc91bb36e4#39057f85a408a40dc702a9e54fd1b9dc91bb36e4"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=873415478ed58686c98df578e2c39d07ddce6773#873415478ed58686c98df578e2c39d07ddce6773"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=39057f85a408a40dc702a9e54fd1b9dc91bb36e4#39057f85a408a40dc702a9e54fd1b9dc91bb36e4"dependencies = ["anyhow","bytes",@@ -1159,7 +1159,7 @@ dependencies = [[[package]]name = "client-api"version = "0.2.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=873415478ed58686c98df578e2c39d07ddce6773#873415478ed58686c98df578e2c39d07ddce6773"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=39057f85a408a40dc702a9e54fd1b9dc91bb36e4#39057f85a408a40dc702a9e54fd1b9dc91bb36e4"dependencies = ["again","anyhow",@@ -1214,7 +1214,7 @@ dependencies = [[[package]]name = "client-api-entity"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=873415478ed58686c98df578e2c39d07ddce6773#873415478ed58686c98df578e2c39d07ddce6773"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=39057f85a408a40dc702a9e54fd1b9dc91bb36e4#39057f85a408a40dc702a9e54fd1b9dc91bb36e4"dependencies = ["collab-entity","collab-rt-entity",@@ -1227,7 +1227,7 @@ dependencies = [[[package]]name = "client-websocket"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=873415478ed58686c98df578e2c39d07ddce6773#873415478ed58686c98df578e2c39d07ddce6773"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=39057f85a408a40dc702a9e54fd1b9dc91bb36e4#39057f85a408a40dc702a9e54fd1b9dc91bb36e4"dependencies = ["futures-channel","futures-util",@@ -1499,7 +1499,7 @@ dependencies = [[[package]]name = "collab-rt-entity"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=873415478ed58686c98df578e2c39d07ddce6773#873415478ed58686c98df578e2c39d07ddce6773"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=39057f85a408a40dc702a9e54fd1b9dc91bb36e4#39057f85a408a40dc702a9e54fd1b9dc91bb36e4"dependencies = ["anyhow","bincode",@@ -1521,7 +1521,7 @@ dependencies = [[[package]]name = "collab-rt-protocol"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=873415478ed58686c98df578e2c39d07ddce6773#873415478ed58686c98df578e2c39d07ddce6773"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=39057f85a408a40dc702a9e54fd1b9dc91bb36e4#39057f85a408a40dc702a9e54fd1b9dc91bb36e4"dependencies = ["anyhow","async-trait",@@ -1786,7 +1786,7 @@ dependencies = ["cssparser-macros","dtoa-short","itoa",- "phf 0.11.2",+ "phf 0.8.0","smallvec",]@@ -1969,7 +1969,7 @@ checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308"[[package]]name = "database-entity"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=873415478ed58686c98df578e2c39d07ddce6773#873415478ed58686c98df578e2c39d07ddce6773"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=39057f85a408a40dc702a9e54fd1b9dc91bb36e4#39057f85a408a40dc702a9e54fd1b9dc91bb36e4"dependencies = ["bincode","bytes",@@ -3459,7 +3459,7 @@ dependencies = [[[package]]name = "gotrue"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=873415478ed58686c98df578e2c39d07ddce6773#873415478ed58686c98df578e2c39d07ddce6773"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=39057f85a408a40dc702a9e54fd1b9dc91bb36e4#39057f85a408a40dc702a9e54fd1b9dc91bb36e4"dependencies = ["anyhow","getrandom 0.2.10",@@ -3474,7 +3474,7 @@ dependencies = [[[package]]name = "gotrue-entity"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=873415478ed58686c98df578e2c39d07ddce6773#873415478ed58686c98df578e2c39d07ddce6773"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=39057f85a408a40dc702a9e54fd1b9dc91bb36e4#39057f85a408a40dc702a9e54fd1b9dc91bb36e4"dependencies = ["app-error","jsonwebtoken",@@ -4098,7 +4098,7 @@ dependencies = [[[package]]name = "infra"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=873415478ed58686c98df578e2c39d07ddce6773#873415478ed58686c98df578e2c39d07ddce6773"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=39057f85a408a40dc702a9e54fd1b9dc91bb36e4#39057f85a408a40dc702a9e54fd1b9dc91bb36e4"dependencies = ["anyhow","bytes",@@ -5189,7 +5189,7 @@ version = "0.8.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12"dependencies = [- "phf_macros 0.8.0",+ "phf_macros","phf_shared 0.8.0","proc-macro-hack",]@@ -5209,7 +5209,6 @@ version = "0.11.2"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc"dependencies = [- "phf_macros 0.11.3","phf_shared 0.11.2",]@@ -5277,19 +5276,6 @@ dependencies = ["syn 1.0.109",]-[[package]]-name = "phf_macros"-version = "0.11.3"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "f84ac04429c13a7ff43785d75ad27569f2951ce0ffd30a3321230db2fc727216"-dependencies = [- "phf_generator 0.11.2",- "phf_shared 0.11.2",- "proc-macro2",- "quote",- "syn 2.0.94",-]-[[package]]name = "phf_shared"version = "0.8.0"@@ -6784,7 +6770,7 @@ dependencies = [[[package]]name = "shared-entity"version = "0.1.0"-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=873415478ed58686c98df578e2c39d07ddce6773#873415478ed58686c98df578e2c39d07ddce6773"+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=39057f85a408a40dc702a9e54fd1b9dc91bb36e4#39057f85a408a40dc702a9e54fd1b9dc91bb36e4"dependencies = ["anyhow","app-error",diff --git a/frontend/rust-lib/Cargo.toml b/frontend/rust-lib/Cargo.tomlindex 8d8e11f6a0..f1d811bf2d 100644--- a/frontend/rust-lib/Cargo.toml+++ b/frontend/rust-lib/Cargo.toml@@ -105,8 +105,8 @@ tantivy = { version = "0.24.0" }# 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 = "873415478ed58686c98df578e2c39d07ddce6773" }-client-api-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "873415478ed58686c98df578e2c39d07ddce6773" }+client-api = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "39057f85a408a40dc702a9e54fd1b9dc91bb36e4" }+client-api-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "39057f85a408a40dc702a9e54fd1b9dc91bb36e4" }[profile.dev]opt-level = 0diff --git a/frontend/rust-lib/flowy-ai/src/persistence/chat_message_sql.rs b/frontend/rust-lib/flowy-ai/src/persistence/chat_message_sql.rsindex aa4dd8215d..6eaf6798e3 100644--- a/frontend/rust-lib/flowy-ai/src/persistence/chat_message_sql.rs+++ b/frontend/rust-lib/flowy-ai/src/persistence/chat_message_sql.rs@@ -65,7 +65,10 @@ pub fn select_chat_messages(query = query.filter(chat_message_table::message_id.lt(before_message_id));}query = query- .order((chat_message_table::message_id.desc(),))+ .order((+ chat_message_table::created_at.desc(),+ chat_message_table::message_id.desc(),+ )).limit(limit_val);let messages: Vec<ChatMessageTable> = query.load::<ChatMessageTable>(&mut *conn)?;diff --git a/frontend/rust-lib/flowy-server/src/lib.rs b/frontend/rust-lib/flowy-server/src/lib.rsindex 33f4b0c0d8..034991a984 100644--- a/frontend/rust-lib/flowy-server/src/lib.rs+++ b/frontend/rust-lib/flowy-server/src/lib.rs@@ -5,5 +5,4 @@ pub mod local_server;mod response;mod server;-mod default_impl;pub mod util;diff --git a/frontend/rust-lib/flowy-server/src/default_impl.rs b/frontend/rust-lib/flowy-server/src/local_server/impls/chat.rssimilarity index 83%rename from frontend/rust-lib/flowy-server/src/default_impl.rsrename to frontend/rust-lib/flowy-server/src/local_server/impls/chat.rsindex 02e313115f..a68dbc8b4f 100644--- a/frontend/rust-lib/flowy-server/src/default_impl.rs+++ b/frontend/rust-lib/flowy-server/src/local_server/impls/chat.rs@@ -6,15 +6,16 @@ use flowy_ai_pub::cloud::{};use flowy_error::FlowyError;use lib_infra::async_trait::async_trait;+use lib_infra::util::timestamp;use serde_json::Value;use std::collections::HashMap;use std::path::Path;use uuid::Uuid;-pub(crate) struct DefaultChatCloudServiceImpl;+pub(crate) struct LocalServerChatServiceImpl;#[async_trait]-impl ChatCloudService for DefaultChatCloudServiceImpl {+impl ChatCloudService for LocalServerChatServiceImpl {async fn create_chat(&self,_uid: &i64,@@ -22,29 +23,40 @@ impl ChatCloudService for DefaultChatCloudServiceImpl {_chat_id: &Uuid,_rag_ids: Vec<Uuid>,) -> Result<(), FlowyError> {- Err(FlowyError::not_support().with_context("Chat is not supported in local server."))+ Ok(())}async fn create_question(&self,_workspace_id: &Uuid,_chat_id: &Uuid,- _message: &str,- _message_type: ChatMessageType,+ message: &str,+ message_type: ChatMessageType,_metadata: &[ChatMessageMetadata],) -> Result<ChatMessage, FlowyError> {- Err(FlowyError::not_support().with_context("Chat is not supported in local server."))+ match message_type {+ ChatMessageType::System => Ok(ChatMessage::new_system(timestamp(), message.to_string())),+ ChatMessageType::User => Ok(ChatMessage::new_human(+ timestamp(),+ message.to_string(),+ None,+ )),+ }}async fn create_answer(&self,_workspace_id: &Uuid,_chat_id: &Uuid,- _message: &str,- _question_id: i64,- _metadata: Option<serde_json::Value>,+ message: &str,+ question_id: i64,+ metadata: Option<serde_json::Value>,) -> Result<ChatMessage, FlowyError> {- Err(FlowyError::not_support().with_context("Chat is not supported in local server."))+ let mut message = ChatMessage::new_ai(timestamp(), message.to_string(), Some(question_id));+ if let Some(metadata) = metadata {+ message.metadata = metadata;+ }+ Ok(message)}async fn stream_answer(@@ -81,9 +93,12 @@ impl ChatCloudService for DefaultChatCloudServiceImpl {&self,_workspace_id: &Uuid,_chat_id: &Uuid,- _message_id: i64,+ message_id: i64,) -> Result<RepeatedRelatedQuestion, FlowyError> {- Err(FlowyError::not_support().with_context("Chat is not supported in local server."))+ Ok(RepeatedRelatedQuestion {+ message_id,+ items: vec![],+ })}async fn get_answer(diff --git a/frontend/rust-lib/flowy-server/src/local_server/impls/mod.rs b/frontend/rust-lib/flowy-server/src/local_server/impls/mod.rsindex 0280cfbefb..f63265e734 100644--- a/frontend/rust-lib/flowy-server/src/local_server/impls/mod.rs+++ b/frontend/rust-lib/flowy-server/src/local_server/impls/mod.rs@@ -1,8 +1,10 @@+pub(crate) use chat::*;pub(crate) use database::*;pub(crate) use document::*;pub(crate) use folder::*;pub(crate) use user::*;+mod chat;mod database;mod document;mod folder;diff --git a/frontend/rust-lib/flowy-server/src/local_server/server.rs b/frontend/rust-lib/flowy-server/src/local_server/server.rsindex cb8b545c53..280a56cfe0 100644--- a/frontend/rust-lib/flowy-server/src/local_server/server.rs+++ b/frontend/rust-lib/flowy-server/src/local_server/server.rs@@ -1,13 +1,13 @@use flowy_search_pub::cloud::SearchCloudService;use std::sync::Arc;-use tokio::sync::mpsc;-+use flowy_ai_pub::cloud::ChatCloudService;use flowy_database_pub::cloud::{DatabaseAIService, DatabaseCloudService};use flowy_document_pub::cloud::DocumentCloudService;use flowy_error::FlowyError;use flowy_folder_pub::cloud::FolderCloudService;use flowy_storage_pub::cloud::StorageCloudService;+use tokio::sync::mpsc;// use flowy_user::services::database::{// get_user_profile, get_user_workspace, open_collab_db, open_user_db,// };@@ -15,8 +15,9 @@ use flowy_user_pub::cloud::UserCloudService;use flowy_user_pub::entities::*;use crate::local_server::impls::{- LocalServerDatabaseCloudServiceImpl, LocalServerDocumentCloudServiceImpl,- LocalServerFolderCloudServiceImpl, LocalServerUserAuthServiceImpl,+ LocalServerChatServiceImpl, LocalServerDatabaseCloudServiceImpl,+ LocalServerDocumentCloudServiceImpl, LocalServerFolderCloudServiceImpl,+ LocalServerUserAuthServiceImpl,};use crate::AppFlowyServer;@@ -78,4 +79,8 @@ impl AppFlowyServer for LocalServer {fn database_ai_service(&self) -> Option<Arc<dyn DatabaseAIService>> {None}++ fn chat_service(&self) -> Arc<dyn ChatCloudService> {+ Arc::new(LocalServerChatServiceImpl)+ }}diff --git a/frontend/rust-lib/flowy-server/src/server.rs b/frontend/rust-lib/flowy-server/src/server.rsindex ee07eefa5a..4c92fe28d2 100644--- a/frontend/rust-lib/flowy-server/src/server.rs+++ b/frontend/rust-lib/flowy-server/src/server.rs@@ -12,7 +12,6 @@ use tokio_stream::wrappers::WatchStream;#[cfg(feature = "enable_supabase")]use {collab_entity::CollabObject, collab_plugins::cloud_storage::RemoteCollabStorage};-use crate::default_impl::DefaultChatCloudServiceImpl;use flowy_database_pub::cloud::{DatabaseAIService, DatabaseCloudService};use flowy_document_pub::cloud::DocumentCloudService;use flowy_folder_pub::cloud::FolderCloudService;@@ -103,9 +102,7 @@ pub trait AppFlowyServer: Send + Sync + 'static {/// An `Arc` wrapping the `DocumentCloudService` interface.fn document_service(&self) -> Arc<dyn DocumentCloudService>;- fn chat_service(&self) -> Arc<dyn ChatCloudService> {- Arc::new(DefaultChatCloudServiceImpl)- }+ fn chat_service(&self) -> Arc<dyn ChatCloudService>;/// Bridge for the Cloud AI Search features///