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
refactor: save chat and chat message
Nathan31d8653ba61f
diff --git a/frontend/rust-lib/event-integration-test/tests/chat/chat_message_test.rs b/frontend/rust-lib/event-integration-test/tests/chat/chat_message_test.rsindex c5b30d68c3..5b29258142 100644--- a/frontend/rust-lib/event-integration-test/tests/chat/chat_message_test.rs+++ b/frontend/rust-lib/event-integration-test/tests/chat/chat_message_test.rs@@ -27,7 +27,6 @@ async fn af_cloud_create_chat_message_test() {&Uuid::from_str(&chat_id).unwrap(),&format!("hello world {}", i),ChatMessageType::System,- &[],).await.unwrap();@@ -83,7 +82,6 @@ async fn af_cloud_load_remote_system_message_test() {&Uuid::from_str(&chat_id).unwrap(),&format!("hello server {}", i),ChatMessageType::System,- &[],).await.unwrap();diff --git a/frontend/rust-lib/event-integration-test/tests/sql_test/chat_message_test.rs b/frontend/rust-lib/event-integration-test/tests/sql_test/chat_message_test.rsindex 549548ccf1..3294ad26db 100644--- a/frontend/rust-lib/event-integration-test/tests/sql_test/chat_message_test.rs+++ b/frontend/rust-lib/event-integration-test/tests/sql_test/chat_message_test.rs@@ -2,8 +2,8 @@ use event_integration_test::user_event::use_localhost_af_cloud;use event_integration_test::EventIntegrationTest;use flowy_ai_pub::cloud::MessageCursor;use flowy_ai_pub::persistence::{- insert_chat_messages, select_chat_messages, select_message, select_message_content,- select_message_where_match_reply_message_id, total_message_count, ChatMessageTable,+ select_answer_where_match_reply_message_id, select_chat_messages, select_message,+ select_message_content, total_message_count, upsert_chat_messages, ChatMessageTable,};use uuid::Uuid;@@ -31,6 +31,7 @@ async fn chat_message_table_insert_select_test() {author_id: "user_1".to_string(),reply_message_id: None,metadata: None,+ is_sync: false,},ChatMessageTable {message_id: message_id_2,@@ -41,11 +42,12 @@ async fn chat_message_table_insert_select_test() {author_id: "ai".to_string(),reply_message_id: Some(message_id_1),metadata: Some(r#"{"source": "test"}"#.to_string()),+ is_sync: false,},];// Test insert_chat_messages- let result = insert_chat_messages(db_conn, &messages);+ let result = upsert_chat_messages(db_conn, &messages);assert!(result.is_ok(),"Failed to insert chat messages: {:?}",@@ -105,11 +107,12 @@ async fn chat_message_table_cursor_test() {author_id: "user_1".to_string(),reply_message_id: None,metadata: None,+ is_sync: false,});}// Insert messages- insert_chat_messages(db_conn, &messages).unwrap();+ upsert_chat_messages(db_conn, &messages).unwrap();// Test MessageCursor::Offsetlet db_conn = test.user_manager.db_connection(uid).unwrap();@@ -173,6 +176,7 @@ async fn chat_message_total_count_test() {author_id: "user_1".to_string(),reply_message_id: None,metadata: None,+ is_sync: false,},ChatMessageTable {message_id: 1002,@@ -183,11 +187,12 @@ async fn chat_message_total_count_test() {author_id: "ai".to_string(),reply_message_id: None,metadata: None,+ is_sync: false,},];// Insert messages- insert_chat_messages(db_conn, &messages).unwrap();+ upsert_chat_messages(db_conn, &messages).unwrap();// Test total_message_countlet db_conn = test.user_manager.db_connection(uid).unwrap();@@ -205,9 +210,10 @@ async fn chat_message_total_count_test() {author_id: "user_1".to_string(),reply_message_id: None,metadata: None,+ is_sync: false,};- insert_chat_messages(db_conn, &[additional_message]).unwrap();+ upsert_chat_messages(db_conn, &[additional_message]).unwrap();// Verify count increasedlet db_conn = test.user_manager.db_connection(uid).unwrap();@@ -242,10 +248,11 @@ async fn chat_message_select_message_test() {author_id: "user_1".to_string(),reply_message_id: None,metadata: Some(r#"{"test_key": "test_value"}"#.to_string()),+ is_sync: false,};// Insert message- insert_chat_messages(db_conn, &[message]).unwrap();+ upsert_chat_messages(db_conn, &[message]).unwrap();// Test select_messagelet db_conn = test.user_manager.db_connection(uid).unwrap();@@ -294,10 +301,11 @@ async fn chat_message_select_content_test() {author_id: "user_1".to_string(),reply_message_id: None,metadata: None,+ is_sync: false,};// Insert message- insert_chat_messages(db_conn, &[message]).unwrap();+ upsert_chat_messages(db_conn, &[message]).unwrap();// Test select_message_contentlet db_conn = test.user_manager.db_connection(uid).unwrap();@@ -334,6 +342,7 @@ async fn chat_message_reply_test() {author_id: "user_1".to_string(),reply_message_id: None,metadata: None,+ is_sync: false,};let answer = ChatMessageTable {@@ -345,14 +354,15 @@ async fn chat_message_reply_test() {author_id: "ai".to_string(),reply_message_id: Some(question_id), // Link to questionmetadata: None,+ is_sync: false,};// Insert messages- insert_chat_messages(db_conn, &[question, answer]).unwrap();+ upsert_chat_messages(db_conn, &[question, answer]).unwrap();// Test select_message_where_match_reply_message_idlet db_conn = test.user_manager.db_connection(uid).unwrap();- let result = select_message_where_match_reply_message_id(db_conn, &chat_id, question_id).unwrap();+ let result = select_answer_where_match_reply_message_id(db_conn, &chat_id, question_id).unwrap();assert!(result.is_some());let reply = result.unwrap();@@ -362,7 +372,7 @@ async fn chat_message_reply_test() {// Test with non-existent reply relationlet db_conn = test.user_manager.db_connection(uid).unwrap();- let no_reply = select_message_where_match_reply_message_id(+ let no_reply = select_answer_where_match_reply_message_id(db_conn, &chat_id, 9999, // Non-existent question ID).unwrap();@@ -372,7 +382,7 @@ async fn chat_message_reply_test() {// Test with wrong chat_idlet db_conn = test.user_manager.db_connection(uid).unwrap();let wrong_chat =- select_message_where_match_reply_message_id(db_conn, "wrong_chat_id", question_id).unwrap();+ select_answer_where_match_reply_message_id(db_conn, "wrong_chat_id", question_id).unwrap();assert!(wrong_chat.is_none());}@@ -399,10 +409,11 @@ async fn chat_message_upsert_test() {author_id: "user_1".to_string(),reply_message_id: None,metadata: None,+ is_sync: false,};// Insert message- insert_chat_messages(db_conn, &[message]).unwrap();+ upsert_chat_messages(db_conn, &[message]).unwrap();// Check original contentlet db_conn = test.user_manager.db_connection(uid).unwrap();@@ -420,10 +431,11 @@ async fn chat_message_upsert_test() {author_id: "user_1".to_string(),reply_message_id: Some(1000), // Added reply IDmetadata: Some(r#"{"updated": true}"#.to_string()),+ is_sync: false,};// Upsert message- insert_chat_messages(db_conn, &[updated_message]).unwrap();+ upsert_chat_messages(db_conn, &[updated_message]).unwrap();// Verify updatelet db_conn = test.user_manager.db_connection(uid).unwrap();@@ -474,11 +486,12 @@ async fn chat_message_select_with_large_dataset() {} else {None},+ is_sync: false,});}// Insert all 100 messages- insert_chat_messages(db_conn, &messages).unwrap();+ upsert_chat_messages(db_conn, &messages).unwrap();// Verify total countlet db_conn = test.user_manager.db_connection(uid).unwrap();diff --git a/frontend/rust-lib/flowy-ai-pub/src/cloud.rs b/frontend/rust-lib/flowy-ai-pub/src/cloud.rsindex 0acf3c5bd3..b91021142e 100644--- a/frontend/rust-lib/flowy-ai-pub/src/cloud.rs+++ b/frontend/rust-lib/flowy-ai-pub/src/cloud.rs@@ -95,7 +95,6 @@ pub trait ChatCloudService: Send + Sync + 'static {chat_id: &Uuid,message: &str,message_type: ChatMessageType,- metadata: &[ChatMessageMetadata],) -> Result<ChatMessage, FlowyError>;async fn create_answer(@@ -111,7 +110,7 @@ pub trait ChatCloudService: Send + Sync + 'static {&self,workspace_id: &Uuid,chat_id: &Uuid,- message_id: i64,+ question_id: i64,format: ResponseFormat,ai_model: Option<AIModel>,) -> Result<StreamAnswer, FlowyError>;@@ -120,7 +119,7 @@ pub trait ChatCloudService: Send + Sync + 'static {&self,workspace_id: &Uuid,chat_id: &Uuid,- question_message_id: i64,+ question_id: i64,) -> Result<ChatMessage, FlowyError>;async fn get_chat_messages(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 f5f985575a..230e5761d2 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,4 +1,5 @@use crate::cloud::MessageCursor;+use client_api::entity::chat_dto::ChatMessage;use flowy_error::{FlowyError, FlowyResult};use flowy_sqlite::upsert::excluded;use flowy_sqlite::{@@ -21,9 +22,40 @@ pub struct ChatMessageTable {pub author_id: String,pub reply_message_id: Option<i64>,pub metadata: Option<String>,+ pub is_sync: bool,+}+impl ChatMessageTable {+ pub fn from_message(chat_id: String, message: ChatMessage, is_sync: bool) -> Self {+ ChatMessageTable {+ message_id: message.message_id,+ chat_id,+ content: message.content,+ created_at: message.created_at.timestamp(),+ author_type: message.author.author_type as i64,+ author_id: message.author.author_id.to_string(),+ reply_message_id: message.reply_message_id,+ metadata: Some(serde_json::to_string(&message.metadata).unwrap_or_default()),+ is_sync,+ }+ }+}++pub fn update_chat_message_is_sync(+ mut conn: DBConnection,+ chat_id_val: &str,+ message_id_val: i64,+ is_sync_val: bool,+) -> FlowyResult<()> {+ diesel::update(chat_message_table::table)+ .filter(chat_message_table::chat_id.eq(chat_id_val))+ .filter(chat_message_table::message_id.eq(message_id_val))+ .set(chat_message_table::is_sync.eq(is_sync_val))+ .execute(&mut *conn)?;++ Ok(())}-pub fn insert_chat_messages(+pub fn upsert_chat_messages(mut conn: DBConnection,new_messages: &[ChatMessageTable],) -> FlowyResult<()> {@@ -143,7 +175,7 @@ pub fn select_message_content(Ok(message)}-pub fn select_message_where_match_reply_message_id(+pub fn select_answer_where_match_reply_message_id(mut conn: DBConnection,chat_id: &str,answer_message_id_val: i64,diff --git a/frontend/rust-lib/flowy-ai-pub/src/persistence/chat_sql.rs b/frontend/rust-lib/flowy-ai-pub/src/persistence/chat_sql.rsindex 218f6222c3..f5398c48c0 100644--- a/frontend/rust-lib/flowy-ai-pub/src/persistence/chat_sql.rs+++ b/frontend/rust-lib/flowy-ai-pub/src/persistence/chat_sql.rs@@ -7,7 +7,10 @@ use flowy_sqlite::{schema::{chat_table, chat_table::dsl},AsChangeset, DBConnection, ExpressionMethods, Identifiable, Insertable, QueryResult, Queryable,};+use lib_infra::util::timestamp;use serde::{Deserialize, Serialize};+use serde_json::Value;+use uuid::Uuid;#[derive(Clone, Default, Queryable, Insertable, Identifiable)]#[diesel(table_name = chat_table)]@@ -18,6 +21,23 @@ pub struct ChatTable {pub name: String,pub metadata: String,pub rag_ids: Option<String>,+ pub is_sync: bool,+}++impl ChatTable {+ pub fn new(chat_id: String, metadata: Value, rag_ids: Vec<Uuid>, is_sync: bool) -> Self {+ let rag_ids = rag_ids.iter().map(|v| v.to_string()).collect::<Vec<_>>();+ let metadata = serialize_chat_metadata(&metadata);+ let rag_ids = Some(serialize_rag_ids(&rag_ids));+ Self {+ chat_id,+ created_at: timestamp(),+ name: "".to_string(),+ metadata,+ rag_ids,+ is_sync,+ }+ }}#[derive(Debug, Clone, Default, Serialize, Deserialize)]@@ -49,27 +69,7 @@ pub struct ChatTableChangeset {pub name: Option<String>,pub metadata: Option<String>,pub rag_ids: Option<String>,-}--impl ChatTableChangeset {- pub fn from_metadata(metadata: ChatTableMetadata) -> Self {- ChatTableChangeset {- chat_id: Default::default(),- metadata: serde_json::to_string(&metadata).ok(),- name: None,- rag_ids: None,- }- }-- pub fn from_rag_ids(rag_ids: Vec<String>) -> Self {- ChatTableChangeset {- chat_id: Default::default(),- // Serialize the Vec<String> to a JSON array string- rag_ids: Some(serde_json::to_string(&rag_ids).unwrap_or_default()),- name: None,- metadata: None,- }- }+ pub is_sync: Option<bool>,}pub fn serialize_rag_ids(rag_ids: &[String]) -> String {@@ -107,6 +107,7 @@ pub fn upsert_chat(mut conn: DBConnection, new_chat: &ChatTable) -> QueryResult<chat_table::name.eq(excluded(chat_table::name)),chat_table::metadata.eq(excluded(chat_table::metadata)),chat_table::rag_ids.eq(excluded(chat_table::rag_ids)),+ chat_table::is_sync.eq(excluded(chat_table::is_sync)),)).execute(&mut *conn)}@@ -120,6 +121,16 @@ pub fn update_chat(Ok(affected_row)}+pub fn update_chat_is_sync(+ mut conn: DBConnection,+ chat_id_val: &str,+ is_sync_val: bool,+) -> QueryResult<usize> {+ diesel::update(dsl::chat_table.filter(chat_table::chat_id.eq(chat_id_val)))+ .set(chat_table::is_sync.eq(is_sync_val))+ .execute(&mut *conn)+}+pub fn read_chat(mut conn: DBConnection, chat_id_val: &str) -> QueryResult<ChatTable> {let row = dsl::chat_table.filter(chat_table::chat_id.eq(chat_id_val))diff --git a/frontend/rust-lib/flowy-ai/src/ai_manager.rs b/frontend/rust-lib/flowy-ai/src/ai_manager.rsindex c2f5b223c0..7602ff8040 100644--- a/frontend/rust-lib/flowy-ai/src/ai_manager.rs+++ b/frontend/rust-lib/flowy-ai/src/ai_manager.rs@@ -4,10 +4,8 @@ use crate::entities::{FilePB, PredefinedFormatPB, RepeatedRelatedQuestionPB, StreamMessageParams,};use crate::local_ai::controller::{LocalAIController, LocalAISetting};-use crate::middleware::chat_service_mw::AICloudServiceMiddleware;-use flowy_ai_pub::persistence::{- read_chat_metadata, serialize_chat_metadata, serialize_rag_ids, upsert_chat, ChatTable,-};+use crate::middleware::chat_service_mw::ChatServiceMiddleware;+use flowy_ai_pub::persistence::read_chat_metadata;use std::collections::HashMap;use dashmap::DashMap;@@ -72,7 +70,7 @@ struct ServerModelsCache {pub const GLOBAL_ACTIVE_MODEL_KEY: &str = "global_active_model";pub struct AIManager {- pub cloud_service_wm: Arc<AICloudServiceMiddleware>,+ pub cloud_service_wm: Arc<ChatServiceMiddleware>,pub user_service: Arc<dyn AIUserService>,pub external_service: Arc<dyn AIExternalService>,chats: Arc<DashMap<Uuid, Arc<Chat>>>,@@ -97,7 +95,7 @@ impl AIManager {});let external_service = Arc::new(query_service);- let cloud_service_wm = Arc::new(AICloudServiceMiddleware::new(+ let cloud_service_wm = Arc::new(ChatServiceMiddleware::new(user_service.clone(),chat_cloud_service,local_ai.clone(),@@ -226,13 +224,6 @@ impl AIManager {.unwrap_or_default();info!("[Chat] create chat with rag_ids: {:?}", rag_ids);- save_chat(- self.user_service.sqlite_connection(*uid)?,- chat_id,- "",- rag_ids.iter().map(|v| v.to_string()).collect(),- json!({}),- )?;self.cloud_service_wm.create_chat(uid, &workspace_id, chat_id, rag_ids, "", json!({}))@@ -730,28 +721,9 @@ async fn sync_chat_documents(Ok(())}-fn save_chat(- conn: DBConnection,- chat_id: &Uuid,- name: &str,- rag_ids: Vec<String>,- metadata: serde_json::Value,-) -> FlowyResult<()> {- let row = ChatTable {- chat_id: chat_id.to_string(),- created_at: timestamp(),- name: name.to_string(),- metadata: serialize_chat_metadata(&metadata),- rag_ids: Some(serialize_rag_ids(&rag_ids)),- };-- upsert_chat(conn, &row)?;- Ok(())-}-async fn refresh_chat_setting(user_service: &Arc<dyn AIUserService>,- cloud_service: &Arc<AICloudServiceMiddleware>,+ cloud_service: &Arc<ChatServiceMiddleware>,store_preferences: &Arc<KVStorePreferences>,chat_id: &Uuid,) -> FlowyResult<ChatSettings> {diff --git a/frontend/rust-lib/flowy-ai/src/chat.rs b/frontend/rust-lib/flowy-ai/src/chat.rsindex 976acee3e4..ba5ff431e9 100644--- a/frontend/rust-lib/flowy-ai/src/chat.rs+++ b/frontend/rust-lib/flowy-ai/src/chat.rs@@ -3,7 +3,7 @@ use crate::entities::{ChatMessageErrorPB, ChatMessageListPB, ChatMessagePB, PredefinedFormatPB,RepeatedRelatedQuestionPB, StreamMessageParams,};-use crate::middleware::chat_service_mw::AICloudServiceMiddleware;+use crate::middleware::chat_service_mw::ChatServiceMiddleware;use crate::notification::{chat_notification_builder, ChatNotification};use crate::stream_message::StreamMessage;use allo_isolate::Isolate;@@ -11,7 +11,7 @@ use flowy_ai_pub::cloud::{AIModel, ChatCloudService, ChatMessage, MessageCursor, QuestionStreamValue, ResponseFormat,};use flowy_ai_pub::persistence::{- insert_chat_messages, select_chat_messages, select_message_where_match_reply_message_id,+ select_answer_where_match_reply_message_id, select_chat_messages, upsert_chat_messages,ChatMessageTable,};use flowy_error::{ErrorCode, FlowyError, FlowyResult};@@ -35,7 +35,7 @@ pub struct Chat {chat_id: Uuid,uid: i64,user_service: Arc<dyn AIUserService>,- chat_service: Arc<AICloudServiceMiddleware>,+ chat_service: Arc<ChatServiceMiddleware>,prev_message_state: Arc<RwLock<PrevMessageState>>,latest_message_id: Arc<AtomicI64>,stop_stream: Arc<AtomicBool>,@@ -47,7 +47,7 @@ impl Chat {uid: i64,chat_id: Uuid,user_service: Arc<dyn AIUserService>,- chat_service: Arc<AICloudServiceMiddleware>,+ chat_service: Arc<ChatServiceMiddleware>,) -> Chat {Chat {uid,@@ -105,7 +105,6 @@ impl Chat {&self.chat_id,¶ms.message,params.message_type.clone(),- &[],).await.map_err(|err| {@@ -126,7 +125,7 @@ impl Chat {}// Save message to disk- save_and_notify_message(uid, &self.chat_id, &self.user_service, question.clone())?;+ notify_message(&self.chat_id, question.clone())?;let format = params.format.clone().map(Into::into).unwrap_or_default();self.stream_response(params.answer_stream_port,@@ -185,7 +184,7 @@ impl Chat {&self,answer_stream_port: i64,answer_stream_buffer: Arc<Mutex<StringBuffer>>,- uid: i64,+ _uid: i64,workspace_id: Uuid,question_id: i64,format: ResponseFormat,@@ -194,7 +193,6 @@ impl Chat {let stop_stream = self.stop_stream.clone();let chat_id = self.chat_id;let cloud_service = self.chat_service.clone();- let user_service = self.user_service.clone();tokio::spawn(async move {let mut answer_sink = IsolateSink::new(Isolate::new(answer_stream_port));match cloud_service@@ -309,7 +307,7 @@ impl Chat {metadata,).await?;- save_and_notify_message(uid, &chat_id, &user_service, answer)?;+ notify_message(&chat_id, answer)?;Ok::<(), FlowyError>(())});}@@ -442,6 +440,7 @@ impl Chat {user_service.sqlite_connection(uid)?,&chat_id,resp.messages.clone(),+ true,) {error!("Failed to save chat:{} messages: {}", chat_id, err);}@@ -492,7 +491,7 @@ impl Chat {let conn = self.user_service.sqlite_connection(self.uid)?;let local_result =- select_message_where_match_reply_message_id(conn, &chat_id.to_string(), answer_message_id)?+ select_answer_where_match_reply_message_id(conn, &chat_id.to_string(), answer_message_id)?.map(|message| message.message_id);if let Some(message_id) = local_result {@@ -543,7 +542,7 @@ impl Chat {.get_answer(&workspace_id, &self.chat_id, question_message_id).await?;- save_and_notify_message(self.uid, &self.chat_id, &self.user_service, answer.clone())?;+ notify_message(&self.chat_id, answer.clone())?;let pb = ChatMessagePB::from(answer);Ok(pb)}@@ -614,6 +613,7 @@ fn save_chat_message_disk(conn: DBConnection,chat_id: &Uuid,messages: Vec<ChatMessage>,+ is_sync: bool,) -> FlowyResult<()> {let records = messages.into_iter()@@ -626,9 +626,10 @@ fn save_chat_message_disk(author_id: message.author.author_id.to_string(),reply_message_id: message.reply_message_id,metadata: Some(serde_json::to_string(&message.metadata).unwrap_or_default()),+ is_sync,}).collect::<Vec<_>>();- insert_chat_messages(conn, &records)?;+ upsert_chat_messages(conn, &records)?;Ok(())}@@ -665,18 +666,8 @@ impl StringBuffer {}}-pub(crate) fn save_and_notify_message(- uid: i64,- chat_id: &Uuid,- user_service: &Arc<dyn AIUserService>,- message: ChatMessage,-) -> Result<(), FlowyError> {+pub(crate) fn notify_message(chat_id: &Uuid, message: ChatMessage) -> Result<(), FlowyError> {trace!("[Chat] save answer: answer={:?}", message);- save_chat_message_disk(- user_service.sqlite_connection(uid)?,- chat_id,- vec![message.clone()],- )?;let pb = ChatMessagePB::from(message);chat_notification_builder(chat_id, ChatNotification::DidReceiveChatMessage).payload(pb)diff --git a/frontend/rust-lib/flowy-ai/src/lib.rs b/frontend/rust-lib/flowy-ai/src/lib.rsindex 400afd1507..5b582b2577 100644--- a/frontend/rust-lib/flowy-ai/src/lib.rs+++ b/frontend/rust-lib/flowy-ai/src/lib.rs@@ -12,6 +12,7 @@ pub mod local_ai;mod middleware;pub mod notification;+pub mod offline;mod protobuf;mod stream_message;mod util;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 e3c3d671ec..ff5c608f22 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@@ -28,14 +28,14 @@ use std::sync::{Arc, Weak};use tracing::{info, trace};use uuid::Uuid;-pub struct AICloudServiceMiddleware {+pub struct ChatServiceMiddleware {cloud_service: Arc<dyn ChatCloudService>,user_service: Arc<dyn AIUserService>,local_ai: Arc<LocalAIController>,storage_service: Weak<dyn StorageService>,}-impl AICloudServiceMiddleware {+impl ChatServiceMiddleware {pub fn new(user_service: Arc<dyn AIUserService>,cloud_service: Arc<dyn ChatCloudService>,@@ -106,7 +106,7 @@ impl AICloudServiceMiddleware {}#[async_trait]-impl ChatCloudService for AICloudServiceMiddleware {+impl ChatCloudService for ChatServiceMiddleware {async fn create_chat(&self,uid: &i64,@@ -128,11 +128,10 @@ impl ChatCloudService for AICloudServiceMiddleware {chat_id: &Uuid,message: &str,message_type: ChatMessageType,- metadata: &[ChatMessageMetadata],) -> Result<ChatMessage, FlowyError> {self.cloud_service- .create_question(workspace_id, chat_id, message, message_type, metadata)+ .create_question(workspace_id, chat_id, message, message_type).await}@@ -154,7 +153,7 @@ impl ChatCloudService for AICloudServiceMiddleware {&self,workspace_id: &Uuid,chat_id: &Uuid,- message_id: i64,+ question_id: i64,format: ResponseFormat,ai_model: Option<AIModel>,) -> Result<StreamAnswer, FlowyError> {@@ -166,7 +165,7 @@ impl ChatCloudService for AICloudServiceMiddleware {info!("stream_answer use model: {:?}", ai_model);if use_local_ai {if self.local_ai.is_running() {- let content = self.get_message_content(message_id)?;+ let content = self.get_message_content(question_id)?;match self.local_ai.stream_question(@@ -191,7 +190,7 @@ impl ChatCloudService for AICloudServiceMiddleware {} else {self.cloud_service- .stream_answer(workspace_id, chat_id, message_id, format, ai_model)+ .stream_answer(workspace_id, chat_id, question_id, format, ai_model).await}}@@ -200,10 +199,10 @@ impl ChatCloudService for AICloudServiceMiddleware {&self,workspace_id: &Uuid,chat_id: &Uuid,- question_message_id: i64,+ question_id: i64,) -> Result<ChatMessage, FlowyError> {if self.local_ai.is_running() {- let content = self.get_message_content(question_message_id)?;+ let content = self.get_message_content(question_id)?;match self.local_ai.ask_question(&chat_id.to_string(), &content)@@ -212,7 +211,7 @@ impl ChatCloudService for AICloudServiceMiddleware {Ok(answer) => {let message = self.cloud_service- .create_answer(workspace_id, chat_id, &answer, question_message_id, None)+ .create_answer(workspace_id, chat_id, &answer, question_id, None).await?;Ok(message)},@@ -224,7 +223,7 @@ impl ChatCloudService for AICloudServiceMiddleware {} else {self.cloud_service- .get_answer(workspace_id, chat_id, question_message_id)+ .get_answer(workspace_id, chat_id, question_id).await}}diff --git a/frontend/rust-lib/flowy-ai/src/offline/mod.rs b/frontend/rust-lib/flowy-ai/src/offline/mod.rsnew file mode 100644index 0000000000..e55b43fdb2--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/offline/mod.rs@@ -0,0 +1 @@+pub mod offline_message_sync;diff --git a/frontend/rust-lib/flowy-ai/src/offline/offline_message_sync.rs b/frontend/rust-lib/flowy-ai/src/offline/offline_message_sync.rsnew file mode 100644index 0000000000..8d7e8d2e42--- /dev/null+++ b/frontend/rust-lib/flowy-ai/src/offline/offline_message_sync.rs@@ -0,0 +1,258 @@+use crate::ai_manager::AIUserService;+use flowy_ai_pub::cloud::{+ AIModel, ChatCloudService, ChatMessage, ChatMessageType, ChatSettings, CompleteTextParams,+ MessageCursor, ModelList, RepeatedChatMessage, RepeatedRelatedQuestion, ResponseFormat,+ StreamAnswer, StreamComplete, UpdateChatParams,+};+use flowy_ai_pub::persistence::{+ update_chat_is_sync, update_chat_message_is_sync, upsert_chat, upsert_chat_messages,+ ChatMessageTable, ChatTable,+};+use flowy_error::FlowyError;+use lib_infra::async_trait::async_trait;+use serde_json::Value;+use std::collections::HashMap;+use std::path::Path;+use std::sync::Arc;+use uuid::Uuid;++pub struct AutoSyncChatService {+ cloud_service: Arc<dyn ChatCloudService>,+ user_service: Arc<dyn AIUserService>,+}++impl AutoSyncChatService {+ pub fn new(+ cloud_service: Arc<dyn ChatCloudService>,+ user_service: Arc<dyn AIUserService>,+ ) -> Self {+ Self {+ cloud_service,+ user_service,+ }+ }++ async fn upsert_message(+ &self,+ chat_id: &Uuid,+ message: ChatMessage,+ is_sync: bool,+ ) -> Result<(), FlowyError> {+ let uid = self.user_service.user_id()?;+ let conn = self.user_service.sqlite_connection(uid)?;+ let row = ChatMessageTable::from_message(chat_id.to_string(), message, is_sync);+ upsert_chat_messages(conn, &[row])?;+ Ok(())+ }++ #[allow(dead_code)]+ async fn update_message_is_sync(+ &self,+ chat_id: &Uuid,+ message_id: i64,+ ) -> Result<(), FlowyError> {+ let uid = self.user_service.user_id()?;+ let conn = self.user_service.sqlite_connection(uid)?;+ update_chat_message_is_sync(conn, &chat_id.to_string(), message_id, true)?;+ Ok(())+ }+}++#[async_trait]+impl ChatCloudService for AutoSyncChatService {+ async fn create_chat(+ &self,+ uid: &i64,+ workspace_id: &Uuid,+ chat_id: &Uuid,+ rag_ids: Vec<Uuid>,+ name: &str,+ metadata: Value,+ ) -> Result<(), FlowyError> {+ let conn = self.user_service.sqlite_connection(*uid)?;+ let chat = ChatTable::new(+ chat_id.to_string(),+ metadata.clone(),+ rag_ids.clone(),+ false,+ );+ upsert_chat(conn, &chat)?;++ if self+ .cloud_service+ .create_chat(uid, workspace_id, chat_id, rag_ids, name, metadata)+ .await+ .is_ok()+ {+ let conn = self.user_service.sqlite_connection(*uid)?;+ update_chat_is_sync(conn, &chat_id.to_string(), true)?;+ }+ Ok(())+ }++ async fn create_question(+ &self,+ workspace_id: &Uuid,+ chat_id: &Uuid,+ message: &str,+ message_type: ChatMessageType,+ ) -> Result<ChatMessage, FlowyError> {+ let message = self+ .cloud_service+ .create_question(workspace_id, chat_id, message, message_type)+ .await?;+ self.upsert_message(chat_id, message.clone(), true).await?;+ // TODO: implement background sync+ // self+ // .update_message_is_sync(chat_id, message.message_id)+ // .await?;+ Ok(message)+ }++ async fn create_answer(+ &self,+ workspace_id: &Uuid,+ chat_id: &Uuid,+ message: &str,+ question_id: i64,+ metadata: Option<Value>,+ ) -> Result<ChatMessage, FlowyError> {+ let message = self+ .cloud_service+ .create_answer(workspace_id, chat_id, message, question_id, metadata)+ .await?;++ // TODO: implement background sync+ self.upsert_message(chat_id, message.clone(), true).await?;+ Ok(message)+ }++ async fn stream_answer(+ &self,+ workspace_id: &Uuid,+ chat_id: &Uuid,+ question_id: i64,+ format: ResponseFormat,+ ai_model: Option<AIModel>,+ ) -> Result<StreamAnswer, FlowyError> {+ self+ .cloud_service+ .stream_answer(workspace_id, chat_id, question_id, format, ai_model)+ .await+ }++ async fn get_answer(+ &self,+ workspace_id: &Uuid,+ chat_id: &Uuid,+ question_id: i64,+ ) -> Result<ChatMessage, FlowyError> {+ let message = self+ .cloud_service+ .get_answer(workspace_id, chat_id, question_id)+ .await?;++ // TODO: implement background sync+ self.upsert_message(chat_id, message.clone(), true).await?;+ Ok(message)+ }++ async fn get_chat_messages(+ &self,+ workspace_id: &Uuid,+ chat_id: &Uuid,+ offset: MessageCursor,+ limit: u64,+ ) -> Result<RepeatedChatMessage, FlowyError> {+ self+ .cloud_service+ .get_chat_messages(workspace_id, chat_id, offset, limit)+ .await+ }++ async fn get_question_from_answer_id(+ &self,+ workspace_id: &Uuid,+ chat_id: &Uuid,+ answer_message_id: i64,+ ) -> Result<ChatMessage, FlowyError> {+ self+ .cloud_service+ .get_question_from_answer_id(workspace_id, chat_id, answer_message_id)+ .await+ }++ async fn get_related_message(+ &self,+ workspace_id: &Uuid,+ chat_id: &Uuid,+ message_id: i64,+ ai_model: Option<AIModel>,+ ) -> Result<RepeatedRelatedQuestion, FlowyError> {+ self+ .cloud_service+ .get_related_message(workspace_id, chat_id, message_id, ai_model)+ .await+ }++ async fn stream_complete(+ &self,+ workspace_id: &Uuid,+ params: CompleteTextParams,+ ai_model: Option<AIModel>,+ ) -> Result<StreamComplete, FlowyError> {+ self+ .cloud_service+ .stream_complete(workspace_id, params, ai_model)+ .await+ }++ async fn embed_file(+ &self,+ workspace_id: &Uuid,+ file_path: &Path,+ chat_id: &Uuid,+ metadata: Option<HashMap<String, Value>>,+ ) -> Result<(), FlowyError> {+ self+ .cloud_service+ .embed_file(workspace_id, file_path, chat_id, metadata)+ .await+ }++ async fn get_chat_settings(+ &self,+ workspace_id: &Uuid,+ chat_id: &Uuid,+ ) -> Result<ChatSettings, FlowyError> {+ // TODO: implement background sync+ self+ .cloud_service+ .get_chat_settings(workspace_id, chat_id)+ .await+ }++ async fn update_chat_settings(+ &self,+ workspace_id: &Uuid,+ chat_id: &Uuid,+ params: UpdateChatParams,+ ) -> Result<(), FlowyError> {+ // TODO: implement background sync+ self+ .cloud_service+ .update_chat_settings(workspace_id, chat_id, params)+ .await+ }++ async fn get_available_models(&self, workspace_id: &Uuid) -> Result<ModelList, FlowyError> {+ self.cloud_service.get_available_models(workspace_id).await+ }++ async fn get_workspace_default_model(&self, workspace_id: &Uuid) -> Result<String, FlowyError> {+ self+ .cloud_service+ .get_workspace_default_model(workspace_id)+ .await+ }+}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 e4bffbfb69..3a7d648133 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@@ -14,9 +14,9 @@ use flowy_ai_pub::cloud::search_dto::{SearchDocumentResponseItem, SearchResult, SearchSummaryResult,};use flowy_ai_pub::cloud::{- AIModel, ChatCloudService, ChatMessage, ChatMessageMetadata, ChatMessageType, ChatSettings,- CompleteTextParams, MessageCursor, ModelList, RepeatedChatMessage, ResponseFormat, StreamAnswer,- StreamComplete, UpdateChatParams,+ AIModel, ChatCloudService, ChatMessage, ChatMessageType, ChatSettings, CompleteTextParams,+ MessageCursor, ModelList, RepeatedChatMessage, ResponseFormat, StreamAnswer, StreamComplete,+ UpdateChatParams,};use flowy_database_pub::cloud::{DatabaseAIService, DatabaseCloudService, DatabaseSnapshot, EncodeCollabByOid, SummaryRowContent,@@ -683,13 +683,12 @@ impl ChatCloudService for ServerProvider {chat_id: &Uuid,message: &str,message_type: ChatMessageType,- metadata: &[ChatMessageMetadata],) -> Result<ChatMessage, FlowyError> {let message = message.to_string();self.get_server()?.chat_service()- .create_question(workspace_id, chat_id, &message, message_type, metadata)+ .create_question(workspace_id, chat_id, &message, message_type).await}@@ -712,14 +711,14 @@ impl ChatCloudService for ServerProvider {&self,workspace_id: &Uuid,chat_id: &Uuid,- message_id: i64,+ question_id: i64,format: ResponseFormat,ai_model: Option<AIModel>,) -> Result<StreamAnswer, FlowyError> {let server = self.get_server()?;server.chat_service()- .stream_answer(workspace_id, chat_id, message_id, format, ai_model)+ .stream_answer(workspace_id, chat_id, question_id, format, ai_model).await}@@ -768,12 +767,12 @@ impl ChatCloudService for ServerProvider {&self,workspace_id: &Uuid,chat_id: &Uuid,- question_message_id: i64,+ question_id: i64,) -> Result<ChatMessage, FlowyError> {let server = self.get_server();server?.chat_service()- .get_answer(workspace_id, chat_id, question_message_id)+ .get_answer(workspace_id, chat_id, question_id).await}diff --git a/frontend/rust-lib/flowy-core/src/lib.rs b/frontend/rust-lib/flowy-core/src/lib.rsindex 1cf748d089..dbbce02136 100644--- a/frontend/rust-lib/flowy-core/src/lib.rs+++ b/frontend/rust-lib/flowy-core/src/lib.rs@@ -8,7 +8,7 @@ use flowy_error::{FlowyError, FlowyResult};use flowy_folder::manager::FolderManager;use flowy_search::folder::indexer::FolderIndexManagerImpl;use flowy_search::services::manager::SearchManager;-use flowy_server::af_cloud::define::ServerUser;+use flowy_server::af_cloud::define::LoginUserService;use std::path::PathBuf;use std::sync::{Arc, Weak};use std::time::Duration;@@ -336,7 +336,7 @@ impl ServerUserImpl {}#[async_trait]-impl ServerUser for ServerUserImpl {+impl LoginUserService for ServerUserImpl {fn workspace_id(&self) -> FlowyResult<Uuid> {self.upgrade_user()?.workspace_id()}diff --git a/frontend/rust-lib/flowy-core/src/server_layer.rs b/frontend/rust-lib/flowy-core/src/server_layer.rsindex 3f5d16b165..a3d6de7003 100644--- a/frontend/rust-lib/flowy-core/src/server_layer.rs+++ b/frontend/rust-lib/flowy-core/src/server_layer.rs@@ -2,24 +2,19 @@ use crate::AppFlowyCoreConfig;use af_plugin::manager::PluginManager;use arc_swap::ArcSwapOption;use dashmap::DashMap;-use flowy_ai::ai_manager::AIUserService;use flowy_ai::local_ai::controller::LocalAIController;use flowy_error::{FlowyError, FlowyResult};-use flowy_server::af_cloud::define::ServerUser;+use flowy_server::af_cloud::define::{AIUserServiceImpl, LoginUserService};use flowy_server::af_cloud::AppFlowyCloudServer;use flowy_server::local_server::LocalServer;use flowy_server::{AppFlowyEncryption, AppFlowyServer, EncryptionImpl};use flowy_server_pub::AuthenticatorType;use flowy_sqlite::kv::KVStorePreferences;-use flowy_sqlite::DBConnection;use flowy_user_pub::entities::*;-use lib_infra::async_trait::async_trait;use serde_repr::*;use std::fmt::{Display, Formatter};-use std::path::PathBuf;use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};use std::sync::{Arc, Weak};-use uuid::Uuid;#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize_repr, Deserialize_repr)]#[repr(u8)]@@ -61,7 +56,7 @@ pub struct ServerProvider {/// The authenticator type of the user.authenticator: AtomicU8,- user: Arc<dyn ServerUser>,+ user: Arc<dyn LoginUserService>,pub(crate) uid: Arc<ArcSwapOption<i64>>,pub local_ai: Arc<LocalAIController>,}@@ -71,7 +66,7 @@ impl ServerProvider {config: AppFlowyCoreConfig,server: Server,store_preferences: Weak<KVStorePreferences>,- server_user: impl ServerUser + 'static,+ server_user: impl LoginUserService + 'static,) -> Self {let user = Arc::new(server_user);let encryption = EncryptionImpl::new(None);@@ -182,28 +177,3 @@ pub fn current_server_type() -> Server {AuthenticatorType::AppFlowyCloud => Server::AppFlowyCloud,}}--struct AIUserServiceImpl(Arc<dyn ServerUser>);--#[async_trait]-impl AIUserService for AIUserServiceImpl {- fn user_id(&self) -> Result<i64, FlowyError> {- self.0.user_id()- }-- async fn is_local_model(&self) -> FlowyResult<bool> {- self.0.is_local_mode().await- }-- fn workspace_id(&self) -> Result<Uuid, FlowyError> {- self.0.workspace_id()- }-- fn sqlite_connection(&self, uid: i64) -> Result<DBConnection, FlowyError> {- self.0.get_sqlite_db(uid)- }-- fn application_root_dir(&self) -> Result<PathBuf, FlowyError> {- self.0.application_root_dir()- }-}diff --git a/frontend/rust-lib/flowy-server/src/af_cloud/define.rs b/frontend/rust-lib/flowy-server/src/af_cloud/define.rsindex e6d8a7fac2..0cebe3371f 100644--- a/frontend/rust-lib/flowy-server/src/af_cloud/define.rs+++ b/frontend/rust-lib/flowy-server/src/af_cloud/define.rs@@ -1,7 +1,9 @@+use flowy_ai::ai_manager::AIUserService;use flowy_error::{FlowyError, FlowyResult};use flowy_sqlite::DBConnection;use lib_infra::async_trait::async_trait;use std::path::PathBuf;+use std::sync::Arc;use uuid::Uuid;pub const USER_SIGN_IN_URL: &str = "sign_in_url";@@ -11,7 +13,7 @@ pub const USER_DEVICE_ID: &str = "device_id";/// Represents a user that is currently using the server.#[async_trait]-pub trait ServerUser: Send + Sync {+pub trait LoginUserService: Send + Sync {/// different user might return different workspace id.fn workspace_id(&self) -> FlowyResult<Uuid>;@@ -21,3 +23,28 @@ pub trait ServerUser: Send + Sync {fn get_sqlite_db(&self, uid: i64) -> Result<DBConnection, FlowyError>;fn application_root_dir(&self) -> Result<PathBuf, FlowyError>;}++pub struct AIUserServiceImpl(pub Arc<dyn LoginUserService>);++#[async_trait]+impl AIUserService for AIUserServiceImpl {+ fn user_id(&self) -> Result<i64, FlowyError> {+ self.0.user_id()+ }++ async fn is_local_model(&self) -> FlowyResult<bool> {+ self.0.is_local_mode().await+ }++ fn workspace_id(&self) -> Result<Uuid, FlowyError> {+ self.0.workspace_id()+ }++ fn sqlite_connection(&self, uid: i64) -> Result<DBConnection, FlowyError> {+ self.0.get_sqlite_db(uid)+ }++ fn application_root_dir(&self) -> Result<PathBuf, FlowyError> {+ self.0.application_root_dir()+ }+}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 3f8803c7fc..14a26078f5 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@@ -8,8 +8,8 @@ use client_api::entity::chat_dto::{RepeatedChatMessage,};use flowy_ai_pub::cloud::{- AIModel, ChatCloudService, ChatMessage, ChatMessageMetadata, ChatMessageType, ChatSettings,- ModelList, StreamAnswer, StreamComplete, UpdateChatParams,+ AIModel, ChatCloudService, ChatMessage, ChatMessageType, ChatSettings, ModelList, StreamAnswer,+ StreamComplete, UpdateChatParams,};use flowy_error::FlowyError;use futures_util::{StreamExt, TryStreamExt};@@ -20,12 +20,12 @@ use std::path::Path;use tracing::trace;use uuid::Uuid;-pub(crate) struct AFCloudChatCloudServiceImpl<T> {+pub(crate) struct CloudChatServiceImpl<T> {pub inner: T,}#[async_trait]-impl<T> ChatCloudService for AFCloudChatCloudServiceImpl<T>+impl<T> ChatCloudService for CloudChatServiceImpl<T>whereT: AFServer,{@@ -59,7 +59,6 @@ wherechat_id: &Uuid,message: &str,message_type: ChatMessageType,- metadata: &[ChatMessageMetadata],) -> Result<ChatMessage, FlowyError> {let chat_id = chat_id.to_string();let try_get_client = self.inner.try_get_client();@@ -132,15 +131,11 @@ where&self,workspace_id: &Uuid,chat_id: &Uuid,- question_message_id: i64,+ question_id: i64,) -> Result<ChatMessage, FlowyError> {let try_get_client = self.inner.try_get_client();let resp = try_get_client?- .get_answer(- workspace_id,- chat_id.to_string().as_str(),- question_message_id,- )+ .get_answer(workspace_id, chat_id.to_string().as_str(), question_id).await.map_err(FlowyError::from)?;Ok(resp)diff --git a/frontend/rust-lib/flowy-server/src/af_cloud/impls/database.rs b/frontend/rust-lib/flowy-server/src/af_cloud/impls/database.rsindex c493dda344..d6a22a2f73 100644--- a/frontend/rust-lib/flowy-server/src/af_cloud/impls/database.rs+++ b/frontend/rust-lib/flowy-server/src/af_cloud/impls/database.rs@@ -1,5 +1,5 @@#![allow(unused_variables)]-use crate::af_cloud::define::ServerUser;+use crate::af_cloud::define::LoginUserService;use crate::af_cloud::impls::util::check_request_workspace_id_is_match;use crate::af_cloud::AFServer;use client_api::entity::ai_dto::{@@ -23,7 +23,7 @@ use uuid::Uuid;pub(crate) struct AFCloudDatabaseCloudServiceImpl<T> {pub inner: T,- pub user: Arc<dyn ServerUser>,+ pub logged_user: Arc<dyn LoginUserService>,}#[async_trait]@@ -40,7 +40,7 @@ whereworkspace_id: &Uuid,) -> Result<Option<EncodedCollab>, FlowyError> {let try_get_client = self.inner.try_get_client();- let cloned_user = self.user.clone();+ let cloned_user = self.logged_user.clone();let params = QueryCollabParams {workspace_id: *workspace_id,inner: QueryCollab::new(*object_id, collab_type),@@ -95,7 +95,7 @@ whereworkspace_id: &Uuid,) -> Result<EncodeCollabByOid, FlowyError> {let try_get_client = self.inner.try_get_client();- let cloned_user = self.user.clone();+ let cloned_user = self.logged_user.clone();let client = try_get_client?;let params = object_ids.into_iter()diff --git a/frontend/rust-lib/flowy-server/src/af_cloud/impls/document.rs b/frontend/rust-lib/flowy-server/src/af_cloud/impls/document.rsindex 4909d96fef..ae67fc6d26 100644--- a/frontend/rust-lib/flowy-server/src/af_cloud/impls/document.rs+++ b/frontend/rust-lib/flowy-server/src/af_cloud/impls/document.rs@@ -13,13 +13,13 @@ use std::sync::Arc;use tracing::instrument;use uuid::Uuid;-use crate::af_cloud::define::ServerUser;+use crate::af_cloud::define::LoginUserService;use crate::af_cloud::impls::util::check_request_workspace_id_is_match;use crate::af_cloud::AFServer;pub(crate) struct AFCloudDocumentCloudServiceImpl<T> {pub inner: T,- pub user: Arc<dyn ServerUser>,+ pub logged_user: Arc<dyn LoginUserService>,}#[async_trait]@@ -49,7 +49,7 @@ wherecheck_request_workspace_id_is_match(workspace_id,- &self.user,+ &self.logged_user,format!("get document doc state:{}", document_id),)?;@@ -85,7 +85,7 @@ where.to_vec();check_request_workspace_id_is_match(workspace_id,- &self.user,+ &self.logged_user,format!("Get {} document", document_id),)?;let collab = Collab::new_with_source(diff --git a/frontend/rust-lib/flowy-server/src/af_cloud/impls/folder.rs b/frontend/rust-lib/flowy-server/src/af_cloud/impls/folder.rsindex 5b8efa4b32..116651a734 100644--- a/frontend/rust-lib/flowy-server/src/af_cloud/impls/folder.rs+++ b/frontend/rust-lib/flowy-server/src/af_cloud/impls/folder.rs@@ -22,13 +22,13 @@ use flowy_folder_pub::cloud::{use flowy_folder_pub::entities::PublishPayload;use lib_infra::async_trait::async_trait;-use crate::af_cloud::define::ServerUser;+use crate::af_cloud::define::LoginUserService;use crate::af_cloud::impls::util::check_request_workspace_id_is_match;use crate::af_cloud::AFServer;pub(crate) struct AFCloudFolderCloudServiceImpl<T> {pub inner: T,- pub user: Arc<dyn ServerUser>,+ pub logged_user: Arc<dyn LoginUserService>,}#[async_trait]@@ -91,7 +91,7 @@ where) -> Result<Option<FolderData>, FlowyError> {let uid = *uid;let try_get_client = self.inner.try_get_client();- let cloned_user = self.user.clone();+ let cloned_user = self.logged_user.clone();let params = QueryCollabParams {workspace_id: *workspace_id,inner: QueryCollab::new(*workspace_id, CollabType::Folder),@@ -131,7 +131,7 @@ whereobject_id: &Uuid,) -> Result<Vec<u8>, FlowyError> {let try_get_client = self.inner.try_get_client();- let cloned_user = self.user.clone();+ let cloned_user = self.logged_user.clone();let params = QueryCollabParams {workspace_id: *workspace_id,inner: QueryCollab::new(*object_id, collab_type),diff --git a/frontend/rust-lib/flowy-server/src/af_cloud/impls/user/cloud_service_impl.rs b/frontend/rust-lib/flowy-server/src/af_cloud/impls/user/cloud_service_impl.rsindex 6d7d9d743b..0ac4555d6e 100644--- a/frontend/rust-lib/flowy-server/src/af_cloud/impls/user/cloud_service_impl.rs+++ b/frontend/rust-lib/flowy-server/src/af_cloud/impls/user/cloud_service_impl.rs@@ -31,7 +31,7 @@ use lib_infra::async_trait::async_trait;use lib_infra::box_any::BoxAny;use uuid::Uuid;-use crate::af_cloud::define::{ServerUser, USER_SIGN_IN_URL};+use crate::af_cloud::define::{LoginUserService, USER_SIGN_IN_URL};use crate::af_cloud::impls::user::dto::{af_update_from_update_params, from_af_workspace_member, to_af_role, user_profile_from_af_profile,};@@ -44,14 +44,14 @@ use super::dto::{from_af_workspace_invitation_status, to_workspace_invitation_stpub(crate) struct AFCloudUserAuthServiceImpl<T> {server: T,user_change_recv: ArcSwapOption<tokio::sync::mpsc::Receiver<UserUpdate>>,- user: Arc<dyn ServerUser>,+ user: Arc<dyn LoginUserService>,}impl<T> AFCloudUserAuthServiceImpl<T> {pub(crate) fn new(server: T,user_change_recv: tokio::sync::mpsc::Receiver<UserUpdate>,- user: Arc<dyn ServerUser>,+ user: Arc<dyn LoginUserService>,) -> Self {Self {server,diff --git a/frontend/rust-lib/flowy-server/src/af_cloud/impls/util.rs b/frontend/rust-lib/flowy-server/src/af_cloud/impls/util.rsindex 0d91de8412..bedcc90ca0 100644--- a/frontend/rust-lib/flowy-server/src/af_cloud/impls/util.rs+++ b/frontend/rust-lib/flowy-server/src/af_cloud/impls/util.rs@@ -1,4 +1,4 @@-use crate::af_cloud::define::ServerUser;+use crate::af_cloud::define::LoginUserService;use flowy_error::{FlowyError, FlowyResult};use std::sync::Arc;use tracing::warn;@@ -9,7 +9,7 @@ use uuid::Uuid;/// This ensures that the operation is being performed in the correct workspace context, enhancing security.pub fn check_request_workspace_id_is_match(expected_workspace_id: &Uuid,- user: &Arc<dyn ServerUser>,+ user: &Arc<dyn LoginUserService>,action: impl AsRef<str>,) -> FlowyResult<()> {let actual_workspace_id = user.workspace_id()?;diff --git a/frontend/rust-lib/flowy-server/src/af_cloud/server.rs b/frontend/rust-lib/flowy-server/src/af_cloud/server.rsindex 06e56a8c05..bb2d11cdbd 100644--- a/frontend/rust-lib/flowy-server/src/af_cloud/server.rs+++ b/frontend/rust-lib/flowy-server/src/af_cloud/server.rs@@ -2,7 +2,7 @@ use std::sync::atomic::{AtomicBool, Ordering};use std::sync::Arc;use std::time::Duration;-use crate::af_cloud::define::ServerUser;+use crate::af_cloud::define::{AIUserServiceImpl, LoginUserService};use anyhow::Error;use arc_swap::ArcSwap;use client_api::collab_sync::ServerCollabMessage;@@ -24,6 +24,11 @@ use flowy_storage_pub::cloud::StorageCloudService;use flowy_user_pub::cloud::{UserCloudService, UserUpdate};use flowy_user_pub::entities::UserTokenState;+use crate::af_cloud::impls::{+ AFCloudDatabaseCloudServiceImpl, AFCloudDocumentCloudServiceImpl, AFCloudFileStorageServiceImpl,+ AFCloudFolderCloudServiceImpl, AFCloudUserAuthServiceImpl, CloudChatServiceImpl,+};+use flowy_ai::offline::offline_message_sync::AutoSyncChatService;use rand::Rng;use semver::Version;use tokio::select;@@ -34,11 +39,6 @@ use tokio_util::sync::CancellationToken;use tracing::{error, info, warn};use uuid::Uuid;-use crate::af_cloud::impls::{- AFCloudChatCloudServiceImpl, AFCloudDatabaseCloudServiceImpl, AFCloudDocumentCloudServiceImpl,- AFCloudFileStorageServiceImpl, AFCloudFolderCloudServiceImpl, AFCloudUserAuthServiceImpl,-};-use crate::AppFlowyServer;use super::impls::AFCloudSearchCloudServiceImpl;@@ -53,7 +53,7 @@ pub struct AppFlowyCloudServer {network_reachable: Arc<AtomicBool>,pub device_id: String,ws_client: Arc<WSClient>,- user: Arc<dyn ServerUser>,+ logged_user: Arc<dyn LoginUserService>,}impl AppFlowyCloudServer {@@ -62,7 +62,7 @@ impl AppFlowyCloudServer {enable_sync: bool,mut device_id: String,client_version: Version,- user: Arc<dyn ServerUser>,+ auth_user_service: Arc<dyn LoginUserService>,) -> Self {// The device id can't be empty, so we generate a new one if it is.if device_id.is_empty() {@@ -100,7 +100,7 @@ impl AppFlowyCloudServer {network_reachable,device_id,ws_client,- user,+ logged_user: auth_user_service,}}@@ -187,7 +187,7 @@ impl AppFlowyServer for AppFlowyCloudServer {Arc::new(AFCloudUserAuthServiceImpl::new(server,rx,- self.user.clone(),+ self.logged_user.clone(),))}@@ -197,7 +197,7 @@ impl AppFlowyServer for AppFlowyCloudServer {};Arc::new(AFCloudFolderCloudServiceImpl {inner: server,- user: self.user.clone(),+ logged_user: self.logged_user.clone(),})}@@ -207,7 +207,7 @@ impl AppFlowyServer for AppFlowyCloudServer {};Arc::new(AFCloudDatabaseCloudServiceImpl {inner: server,- user: self.user.clone(),+ logged_user: self.logged_user.clone(),})}@@ -217,7 +217,7 @@ impl AppFlowyServer for AppFlowyCloudServer {};Some(Arc::new(AFCloudDatabaseCloudServiceImpl {inner: server,- user: self.user.clone(),+ logged_user: self.logged_user.clone(),}))}@@ -227,7 +227,7 @@ impl AppFlowyServer for AppFlowyCloudServer {};Arc::new(AFCloudDocumentCloudServiceImpl {inner: server,- user: self.user.clone(),+ logged_user: self.logged_user.clone(),})}@@ -235,7 +235,11 @@ impl AppFlowyServer for AppFlowyCloudServer {let server = AFServerImpl {client: self.get_client(),};- Arc::new(AFCloudChatCloudServiceImpl { inner: server })++ Arc::new(AutoSyncChatService::new(+ Arc::new(CloudChatServiceImpl { inner: server }),+ Arc::new(AIUserServiceImpl(self.logged_user.clone())),+ ))}fn subscribe_ws_state(&self) -> Option<WSConnectStateReceiver> {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 9057288f88..8e731edb84 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,4 +1,4 @@-use crate::af_cloud::define::ServerUser;+use crate::af_cloud::define::LoginUserService;use chrono::{TimeZone, Utc};use client_api::entity::ai_dto::RepeatedRelatedQuestion;use client_api::entity::CompletionStream;@@ -6,14 +6,15 @@ 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, ChatMessageMetadata,- ChatMessageType, ChatSettings, CompleteTextParams, MessageCursor, ModelList, RelatedQuestion,- RepeatedChatMessage, ResponseFormat, StreamAnswer, StreamComplete, UpdateChatParams,+ AIModel, AppErrorCode, AppResponseError, ChatCloudService, ChatMessage, ChatMessageType,+ ChatSettings, CompleteTextParams, MessageCursor, ModelList, RelatedQuestion, RepeatedChatMessage,+ ResponseFormat, StreamAnswer, StreamComplete, UpdateChatParams,};use flowy_ai_pub::persistence::{- deserialize_chat_metadata, deserialize_rag_ids, read_chat, select_chat_messages,- select_message_content, select_message_where_match_reply_message_id, serialize_chat_metadata,- serialize_rag_ids, update_chat, upsert_chat, ChatMessageTable, ChatTable, ChatTableChangeset,+ deserialize_chat_metadata, deserialize_rag_ids, read_chat,+ select_answer_where_match_reply_message_id, select_chat_messages, select_message_content,+ serialize_chat_metadata, serialize_rag_ids, update_chat, upsert_chat, upsert_chat_messages,+ ChatMessageTable, ChatTable, ChatTableChangeset,};use flowy_error::{FlowyError, FlowyResult};use futures_util::{stream, StreamExt, TryStreamExt};@@ -26,12 +27,12 @@ use std::sync::Arc;use tracing::trace;use uuid::Uuid;-pub struct LocalServerChatServiceImpl {- pub user: Arc<dyn ServerUser>,+pub struct LocalChatServiceImpl {+ pub user: Arc<dyn LoginUserService>,pub local_ai: Arc<LocalAIController>,}-impl LocalServerChatServiceImpl {+impl LocalChatServiceImpl {fn get_message_content(&self, message_id: i64) -> FlowyResult<String> {let uid = self.user.user_id()?;let db = self.user.get_sqlite_db(uid)?;@@ -40,35 +41,30 @@ impl LocalServerChatServiceImpl {})?;Ok(content)}++ async fn upsert_message(&self, chat_id: &Uuid, message: ChatMessage) -> Result<(), FlowyError> {+ let uid = self.user.user_id()?;+ let conn = self.user.get_sqlite_db(uid)?;+ let row = ChatMessageTable::from_message(chat_id.to_string(), message, true);+ upsert_chat_messages(conn, &[row])?;+ Ok(())+ }}#[async_trait]-impl ChatCloudService for LocalServerChatServiceImpl {+impl ChatCloudService for LocalChatServiceImpl {async fn create_chat(&self,_uid: &i64,_workspace_id: &Uuid,chat_id: &Uuid,rag_ids: Vec<Uuid>,- name: &str,+ _name: &str,metadata: Value,) -> Result<(), FlowyError> {let uid = self.user.user_id()?;let db = self.user.get_sqlite_db(uid)?;-- let rag_ids = rag_ids- .iter()- .map(|v| v.to_string())- .collect::<Vec<String>>();-- let row = ChatTable {- chat_id: chat_id.to_string(),- created_at: timestamp(),- name: name.to_string(),- metadata: serialize_chat_metadata(&metadata),- rag_ids: Some(serialize_rag_ids(&rag_ids)),- };-+ let row = ChatTable::new(chat_id.to_string(), metadata, rag_ids, true);upsert_chat(db, &row)?;Ok(())}@@ -76,25 +72,23 @@ impl ChatCloudService for LocalServerChatServiceImpl {async fn create_question(&self,_workspace_id: &Uuid,- _chat_id: &Uuid,+ chat_id: &Uuid,message: &str,message_type: ChatMessageType,- _metadata: &[ChatMessageMetadata],) -> Result<ChatMessage, FlowyError> {- match message_type {- ChatMessageType::System => Ok(ChatMessage::new_system(timestamp(), message.to_string())),- ChatMessageType::User => Ok(ChatMessage::new_human(- timestamp(),- message.to_string(),- None,- )),- }+ let message = match message_type {+ ChatMessageType::System => ChatMessage::new_system(timestamp(), message.to_string()),+ ChatMessageType::User => ChatMessage::new_human(timestamp(), message.to_string(), None),+ };++ self.upsert_message(chat_id, message.clone()).await?;+ Ok(message)}async fn create_answer(&self,_workspace_id: &Uuid,- _chat_id: &Uuid,+ chat_id: &Uuid,message: &str,question_id: i64,metadata: Option<serde_json::Value>,@@ -103,6 +97,7 @@ impl ChatCloudService for LocalServerChatServiceImpl {if let Some(metadata) = metadata {message.metadata = metadata;}+ self.upsert_message(chat_id, message.clone()).await?;Ok(message)}@@ -131,22 +126,26 @@ impl ChatCloudService for LocalServerChatServiceImpl {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 {- if self.local_ai.is_enabled() {- Err(FlowyError::local_ai_not_ready())- } else {- Err(FlowyError::local_ai_disabled())- }+ Err(FlowyError::local_ai_disabled())}}async fn get_answer(&self,_workspace_id: &Uuid,- _chat_id: &Uuid,- _question_message_id: i64,+ chat_id: &Uuid,+ question_id: i64,) -> Result<ChatMessage, FlowyError> {- Err(FlowyError::not_support().with_context("Chat is not supported in local server."))+ let uid = self.user.user_id()?;+ let db = self.user.get_sqlite_db(uid)?;++ match select_answer_where_match_reply_message_id(db, &chat_id.to_string(), question_id)? {+ None => Err(FlowyError::record_not_found()),+ Some(message) => Ok(chat_message_from_row(message)),+ }}async fn get_chat_messages(@@ -183,7 +182,7 @@ impl ChatCloudService for LocalServerChatServiceImpl {let chat_id = chat_id.to_string();let uid = self.user.user_id()?;let db = self.user.get_sqlite_db(uid)?;- let row = select_message_where_match_reply_message_id(db, &chat_id, answer_message_id)?+ let row = select_answer_where_match_reply_message_id(db, &chat_id, answer_message_id)?.map(chat_message_from_row).ok_or_else(FlowyError::record_not_found)?;Ok(row)@@ -247,12 +246,10 @@ impl ChatCloudService for LocalServerChatServiceImpl {),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 {- if self.local_ai.is_enabled() {- Err(FlowyError::local_ai_not_ready())- } else {- Err(FlowyError::local_ai_disabled())- }+ Err(FlowyError::local_ai_disabled())}}@@ -285,7 +282,7 @@ impl ChatCloudService for LocalServerChatServiceImpl {let db = self.user.get_sqlite_db(uid)?;let row = read_chat(db, &chat_id)?;let rag_ids = deserialize_rag_ids(&row.rag_ids);- let metadata = deserialize_chat_metadata::<serde_json::Value>(&row.metadata);+ let metadata = deserialize_chat_metadata::<Value>(&row.metadata);let setting = ChatSettings {name: row.name,rag_ids,@@ -308,6 +305,7 @@ impl ChatCloudService for LocalServerChatServiceImpl {name: s.name,metadata: s.metadata.map(|s| serialize_chat_metadata(&s)),rag_ids: s.rag_ids.map(|s| serialize_rag_ids(&s)),+ is_sync: None,};update_chat(&mut db, changeset)?;diff --git a/frontend/rust-lib/flowy-server/src/local_server/server.rs b/frontend/rust-lib/flowy-server/src/local_server/server.rsindex 0ef930320e..719dd59c95 100644--- a/frontend/rust-lib/flowy-server/src/local_server/server.rs+++ b/frontend/rust-lib/flowy-server/src/local_server/server.rs@@ -1,11 +1,10 @@use flowy_search_pub::cloud::SearchCloudService;use std::sync::Arc;-use crate::af_cloud::define::ServerUser;+use crate::af_cloud::define::LoginUserService;use crate::local_server::impls::{- LocalServerChatServiceImpl, LocalServerDatabaseCloudServiceImpl,- LocalServerDocumentCloudServiceImpl, LocalServerFolderCloudServiceImpl,- LocalServerUserServiceImpl,+ LocalChatServiceImpl, LocalServerDatabaseCloudServiceImpl, LocalServerDocumentCloudServiceImpl,+ LocalServerFolderCloudServiceImpl, LocalServerUserServiceImpl,};use crate::AppFlowyServer;use flowy_ai::local_ai::controller::LocalAIController;@@ -18,13 +17,13 @@ use flowy_user_pub::cloud::UserCloudService;use tokio::sync::mpsc;pub struct LocalServer {- user: Arc<dyn ServerUser>,+ user: Arc<dyn LoginUserService>,local_ai: Arc<LocalAIController>,stop_tx: Option<mpsc::Sender<()>>,}impl LocalServer {- pub fn new(user: Arc<dyn ServerUser>, local_ai: Arc<LocalAIController>) -> Self {+ pub fn new(user: Arc<dyn LoginUserService>, local_ai: Arc<LocalAIController>) -> Self {Self {user,local_ai,@@ -62,7 +61,7 @@ impl AppFlowyServer for LocalServer {}fn chat_service(&self) -> Arc<dyn ChatCloudService> {- Arc::new(LocalServerChatServiceImpl {+ Arc::new(LocalChatServiceImpl {user: self.user.clone(),local_ai: self.local_ai.clone(),})diff --git a/frontend/rust-lib/flowy-server/tests/af_cloud_test/util.rs b/frontend/rust-lib/flowy-server/tests/af_cloud_test/util.rsindex f196e44ea9..a8dcdb507f 100644--- a/frontend/rust-lib/flowy-server/tests/af_cloud_test/util.rs+++ b/frontend/rust-lib/flowy-server/tests/af_cloud_test/util.rs@@ -8,7 +8,7 @@ use flowy_error::{FlowyError, FlowyResult};use uuid::Uuid;use crate::setup_log;-use flowy_server::af_cloud::define::ServerUser;+use flowy_server::af_cloud::define::LoginUserService;use flowy_server::af_cloud::AppFlowyCloudServer;use flowy_server_pub::af_cloud_config::AFCloudConfiguration;use flowy_sqlite::DBConnection;@@ -42,7 +42,7 @@ pub fn af_cloud_server(config: AFCloudConfiguration) -> Arc<AppFlowyCloudServer>struct FakeServerUserImpl;#[async_trait]-impl ServerUser for FakeServerUserImpl {+impl LoginUserService for FakeServerUserImpl {fn workspace_id(&self) -> FlowyResult<Uuid> {todo!()}diff --git a/frontend/rust-lib/flowy-sqlite/migrations/2025-04-17-142713_offline_chat_message/down.sql b/frontend/rust-lib/flowy-sqlite/migrations/2025-04-17-142713_offline_chat_message/down.sqlnew file mode 100644index 0000000000..65dec0f30a--- /dev/null+++ b/frontend/rust-lib/flowy-sqlite/migrations/2025-04-17-142713_offline_chat_message/down.sql@@ -0,0 +1,3 @@+-- This file should undo anything in `up.sql`+ALTER TABLE chat_table DROP COLUMN is_sync;+ALTER TABLE chat_message_table DROP COLUMN is_sync;diff --git a/frontend/rust-lib/flowy-sqlite/migrations/2025-04-17-142713_offline_chat_message/up.sql b/frontend/rust-lib/flowy-sqlite/migrations/2025-04-17-142713_offline_chat_message/up.sqlnew file mode 100644index 0000000000..ff8dce94bc--- /dev/null+++ b/frontend/rust-lib/flowy-sqlite/migrations/2025-04-17-142713_offline_chat_message/up.sql@@ -0,0 +1,5 @@+-- Your SQL goes here+ALTER TABLE chat_table+ ADD COLUMN is_sync BOOLEAN DEFAULT TRUE NOT NULL;+ALTER TABLE chat_message_table+ ADD COLUMN is_sync BOOLEAN DEFAULT TRUE NOT NULL;\ No newline at end of filediff --git a/frontend/rust-lib/flowy-sqlite/src/schema.rs b/frontend/rust-lib/flowy-sqlite/src/schema.rsindex 9504d126ed..f3caa86e66 100644--- a/frontend/rust-lib/flowy-sqlite/src/schema.rs+++ b/frontend/rust-lib/flowy-sqlite/src/schema.rs@@ -27,6 +27,7 @@ diesel::table! {author_id -> Text,reply_message_id -> Nullable<BigInt>,metadata -> Nullable<Text>,+ is_sync -> Bool,}}@@ -37,6 +38,7 @@ diesel::table! {name -> Text,metadata -> Text,rag_ids -> Nullable<Text>,+ is_sync -> Bool,}}@@ -126,15 +128,15 @@ diesel::table! {}diesel::allow_tables_to_appear_in_same_query!(- af_collab_metadata,- chat_local_setting_table,- chat_message_table,- chat_table,- collab_snapshot,- upload_file_part,- upload_file_table,- user_data_migration_records,- user_table,- user_workspace_table,- workspace_members_table,+ af_collab_metadata,+ chat_local_setting_table,+ chat_message_table,+ chat_table,+ collab_snapshot,+ upload_file_part,+ upload_file_table,+ user_data_migration_records,+ user_table,+ user_workspace_table,+ workspace_members_table,);