2024-06-24 23:59:38 +00:00
|
|
|
use crate::chat_manager::ChatUserService;
|
2024-06-03 06:27:28 +00:00
|
|
|
use crate::entities::{
|
|
|
|
ChatMessageErrorPB, ChatMessageListPB, ChatMessagePB, RepeatedRelatedQuestionPB,
|
|
|
|
};
|
2024-07-25 11:41:16 +00:00
|
|
|
use crate::middleware::chat_service_mw::CloudServiceMiddleware;
|
2024-07-18 12:54:35 +00:00
|
|
|
use crate::notification::{make_notification, ChatNotification};
|
2024-06-09 06:02:32 +00:00
|
|
|
use crate::persistence::{insert_chat_messages, select_chat_messages, ChatMessageTable};
|
|
|
|
use allo_isolate::Isolate;
|
2024-06-14 01:02:06 +00:00
|
|
|
use flowy_chat_pub::cloud::{ChatCloudService, ChatMessage, ChatMessageType, MessageCursor};
|
2024-06-03 06:27:28 +00:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
|
|
|
use flowy_sqlite::DBConnection;
|
2024-06-09 06:02:32 +00:00
|
|
|
use futures::{SinkExt, StreamExt};
|
|
|
|
use lib_infra::isolate_stream::IsolateSink;
|
2024-07-15 07:23:23 +00:00
|
|
|
use std::path::PathBuf;
|
2024-06-09 06:02:32 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicI64};
|
2024-06-03 06:27:28 +00:00
|
|
|
use std::sync::Arc;
|
2024-06-09 06:02:32 +00:00
|
|
|
use tokio::sync::{Mutex, RwLock};
|
2024-06-03 06:27:28 +00:00
|
|
|
use tracing::{error, instrument, trace};
|
|
|
|
|
|
|
|
enum PrevMessageState {
|
|
|
|
HasMore,
|
|
|
|
NoMore,
|
|
|
|
Loading,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Chat {
|
|
|
|
chat_id: String,
|
|
|
|
uid: i64,
|
|
|
|
user_service: Arc<dyn ChatUserService>,
|
2024-07-25 11:41:16 +00:00
|
|
|
chat_service: Arc<CloudServiceMiddleware>,
|
2024-06-03 06:27:28 +00:00
|
|
|
prev_message_state: Arc<RwLock<PrevMessageState>>,
|
|
|
|
latest_message_id: Arc<AtomicI64>,
|
2024-06-09 06:02:32 +00:00
|
|
|
stop_stream: Arc<AtomicBool>,
|
|
|
|
steam_buffer: Arc<Mutex<String>>,
|
2024-06-03 06:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Chat {
|
|
|
|
pub fn new(
|
|
|
|
uid: i64,
|
|
|
|
chat_id: String,
|
|
|
|
user_service: Arc<dyn ChatUserService>,
|
2024-07-25 11:41:16 +00:00
|
|
|
chat_service: Arc<CloudServiceMiddleware>,
|
2024-06-03 06:27:28 +00:00
|
|
|
) -> Chat {
|
|
|
|
Chat {
|
|
|
|
uid,
|
|
|
|
chat_id,
|
2024-06-30 09:38:39 +00:00
|
|
|
chat_service,
|
2024-06-03 06:27:28 +00:00
|
|
|
user_service,
|
|
|
|
prev_message_state: Arc::new(RwLock::new(PrevMessageState::HasMore)),
|
|
|
|
latest_message_id: Default::default(),
|
2024-06-09 06:02:32 +00:00
|
|
|
stop_stream: Arc::new(AtomicBool::new(false)),
|
|
|
|
steam_buffer: Arc::new(Mutex::new("".to_string())),
|
2024-06-03 06:27:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(&self) {}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub async fn pull_latest_message(&self, limit: i64) {
|
|
|
|
let latest_message_id = self
|
|
|
|
.latest_message_id
|
|
|
|
.load(std::sync::atomic::Ordering::Relaxed);
|
|
|
|
if latest_message_id > 0 {
|
|
|
|
let _ = self
|
|
|
|
.load_remote_chat_messages(limit, None, Some(latest_message_id))
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-09 06:02:32 +00:00
|
|
|
pub async fn stop_stream_message(&self) {
|
|
|
|
self
|
|
|
|
.stop_stream
|
|
|
|
.store(true, std::sync::atomic::Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
|
2024-06-03 06:27:28 +00:00
|
|
|
#[instrument(level = "info", skip_all, err)]
|
2024-06-09 06:02:32 +00:00
|
|
|
pub async fn stream_chat_message(
|
2024-06-03 06:27:28 +00:00
|
|
|
&self,
|
|
|
|
message: &str,
|
|
|
|
message_type: ChatMessageType,
|
2024-06-09 06:02:32 +00:00
|
|
|
text_stream_port: i64,
|
|
|
|
) -> Result<ChatMessagePB, FlowyError> {
|
2024-06-03 06:27:28 +00:00
|
|
|
if message.len() > 2000 {
|
|
|
|
return Err(FlowyError::text_too_long().with_context("Exceeds maximum message 2000 length"));
|
|
|
|
}
|
2024-06-09 06:02:32 +00:00
|
|
|
// clear
|
|
|
|
self
|
|
|
|
.stop_stream
|
|
|
|
.store(false, std::sync::atomic::Ordering::SeqCst);
|
|
|
|
self.steam_buffer.lock().await.clear();
|
2024-06-03 06:27:28 +00:00
|
|
|
|
2024-06-09 06:02:32 +00:00
|
|
|
let stream_buffer = self.steam_buffer.clone();
|
2024-06-03 06:27:28 +00:00
|
|
|
let uid = self.user_service.user_id()?;
|
|
|
|
let workspace_id = self.user_service.workspace_id()?;
|
2024-06-09 06:02:32 +00:00
|
|
|
|
|
|
|
let question = self
|
2024-06-30 09:38:39 +00:00
|
|
|
.chat_service
|
|
|
|
.save_question(&workspace_id, &self.chat_id, message, message_type)
|
2024-06-09 06:02:32 +00:00
|
|
|
.await
|
|
|
|
.map_err(|err| {
|
|
|
|
error!("Failed to send question: {}", err);
|
|
|
|
FlowyError::server_error()
|
|
|
|
})?;
|
|
|
|
|
|
|
|
save_chat_message(
|
|
|
|
self.user_service.sqlite_connection(uid)?,
|
|
|
|
&self.chat_id,
|
|
|
|
vec![question.clone()],
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let stop_stream = self.stop_stream.clone();
|
|
|
|
let chat_id = self.chat_id.clone();
|
|
|
|
let question_id = question.message_id;
|
2024-06-30 09:38:39 +00:00
|
|
|
let cloud_service = self.chat_service.clone();
|
2024-06-09 06:02:32 +00:00
|
|
|
let user_service = self.user_service.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
let mut text_sink = IsolateSink::new(Isolate::new(text_stream_port));
|
|
|
|
match cloud_service
|
2024-06-30 09:38:39 +00:00
|
|
|
.ask_question(&workspace_id, &chat_id, question_id)
|
2024-06-09 06:02:32 +00:00
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(mut stream) => {
|
|
|
|
while let Some(message) = stream.next().await {
|
|
|
|
match message {
|
2024-06-14 01:02:06 +00:00
|
|
|
Ok(message) => {
|
|
|
|
if stop_stream.load(std::sync::atomic::Ordering::Relaxed) {
|
|
|
|
trace!("[Chat] stop streaming message");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let s = String::from_utf8(message.to_vec()).unwrap_or_default();
|
|
|
|
stream_buffer.lock().await.push_str(&s);
|
|
|
|
let _ = text_sink.send(format!("data:{}", s)).await;
|
2024-06-09 06:02:32 +00:00
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
error!("[Chat] failed to stream answer: {}", err);
|
|
|
|
let _ = text_sink.send(format!("error:{}", err)).await;
|
|
|
|
let pb = ChatMessageErrorPB {
|
|
|
|
chat_id: chat_id.clone(),
|
|
|
|
error_message: err.to_string(),
|
|
|
|
};
|
2024-07-18 12:54:35 +00:00
|
|
|
make_notification(&chat_id, ChatNotification::StreamChatMessageError)
|
2024-06-09 06:02:32 +00:00
|
|
|
.payload(pb)
|
|
|
|
.send();
|
2024-07-02 05:26:53 +00:00
|
|
|
return Err(err);
|
2024-06-09 06:02:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(err) => {
|
2024-07-02 05:26:53 +00:00
|
|
|
error!("[Chat] failed to stream answer: {}", err);
|
2024-07-24 06:23:09 +00:00
|
|
|
if err.is_ai_response_limit_exceeded() {
|
|
|
|
let _ = text_sink.send("AI_RESPONSE_LIMIT".to_string()).await;
|
|
|
|
} else {
|
|
|
|
let _ = text_sink.send(format!("error:{}", err)).await;
|
|
|
|
}
|
|
|
|
|
2024-06-09 06:02:32 +00:00
|
|
|
let pb = ChatMessageErrorPB {
|
|
|
|
chat_id: chat_id.clone(),
|
|
|
|
error_message: err.to_string(),
|
|
|
|
};
|
2024-07-18 12:54:35 +00:00
|
|
|
make_notification(&chat_id, ChatNotification::StreamChatMessageError)
|
2024-06-09 06:02:32 +00:00
|
|
|
.payload(pb)
|
|
|
|
.send();
|
2024-07-02 05:26:53 +00:00
|
|
|
return Err(err);
|
2024-06-09 06:02:32 +00:00
|
|
|
},
|
|
|
|
}
|
2024-06-14 01:02:06 +00:00
|
|
|
|
2024-07-18 12:54:35 +00:00
|
|
|
make_notification(&chat_id, ChatNotification::FinishStreaming).send();
|
2024-07-02 05:26:53 +00:00
|
|
|
if stream_buffer.lock().await.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2024-06-14 01:02:06 +00:00
|
|
|
let answer = cloud_service
|
|
|
|
.save_answer(
|
|
|
|
&workspace_id,
|
|
|
|
&chat_id,
|
|
|
|
&stream_buffer.lock().await,
|
|
|
|
question_id,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Self::save_answer(uid, &chat_id, &user_service, answer)?;
|
2024-06-09 06:02:32 +00:00
|
|
|
Ok::<(), FlowyError>(())
|
|
|
|
});
|
|
|
|
|
|
|
|
let question_pb = ChatMessagePB::from(question);
|
|
|
|
Ok(question_pb)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_answer(
|
|
|
|
uid: i64,
|
|
|
|
chat_id: &str,
|
|
|
|
user_service: &Arc<dyn ChatUserService>,
|
|
|
|
answer: ChatMessage,
|
|
|
|
) -> Result<(), FlowyError> {
|
|
|
|
save_chat_message(
|
|
|
|
user_service.sqlite_connection(uid)?,
|
|
|
|
chat_id,
|
|
|
|
vec![answer.clone()],
|
|
|
|
)?;
|
|
|
|
let pb = ChatMessagePB::from(answer);
|
2024-07-18 12:54:35 +00:00
|
|
|
make_notification(chat_id, ChatNotification::DidReceiveChatMessage)
|
2024-06-09 06:02:32 +00:00
|
|
|
.payload(pb)
|
|
|
|
.send();
|
2024-06-03 06:27:28 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load chat messages for a given `chat_id`.
|
|
|
|
///
|
|
|
|
/// 1. When opening a chat:
|
|
|
|
/// - Loads local chat messages.
|
|
|
|
/// - `after_message_id` and `before_message_id` are `None`.
|
|
|
|
/// - Spawns a task to load messages from the remote server, notifying the user when the remote messages are loaded.
|
|
|
|
///
|
|
|
|
/// 2. Loading more messages in an existing chat with `after_message_id`:
|
|
|
|
/// - `after_message_id` is the last message ID in the current chat messages.
|
|
|
|
///
|
|
|
|
/// 3. Loading more messages in an existing chat with `before_message_id`:
|
|
|
|
/// - `before_message_id` is the first message ID in the current chat messages.
|
|
|
|
pub async fn load_prev_chat_messages(
|
|
|
|
&self,
|
|
|
|
limit: i64,
|
|
|
|
before_message_id: Option<i64>,
|
|
|
|
) -> Result<ChatMessageListPB, FlowyError> {
|
|
|
|
trace!(
|
2024-06-09 06:02:32 +00:00
|
|
|
"[Chat] Loading messages from disk: chat_id={}, limit={}, before_message_id={:?}",
|
2024-06-03 06:27:28 +00:00
|
|
|
self.chat_id,
|
|
|
|
limit,
|
|
|
|
before_message_id
|
|
|
|
);
|
|
|
|
let messages = self
|
|
|
|
.load_local_chat_messages(limit, None, before_message_id)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// If the number of messages equals the limit, then no need to load more messages from remote
|
|
|
|
if messages.len() == limit as usize {
|
2024-06-09 06:02:32 +00:00
|
|
|
let pb = ChatMessageListPB {
|
2024-06-03 06:27:28 +00:00
|
|
|
messages,
|
2024-06-09 06:02:32 +00:00
|
|
|
has_more: true,
|
2024-06-03 06:27:28 +00:00
|
|
|
total: 0,
|
2024-06-09 06:02:32 +00:00
|
|
|
};
|
2024-07-18 12:54:35 +00:00
|
|
|
make_notification(&self.chat_id, ChatNotification::DidLoadPrevChatMessage)
|
2024-06-09 06:02:32 +00:00
|
|
|
.payload(pb.clone())
|
|
|
|
.send();
|
|
|
|
return Ok(pb);
|
2024-06-03 06:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if matches!(
|
|
|
|
*self.prev_message_state.read().await,
|
|
|
|
PrevMessageState::HasMore
|
|
|
|
) {
|
|
|
|
*self.prev_message_state.write().await = PrevMessageState::Loading;
|
|
|
|
if let Err(err) = self
|
|
|
|
.load_remote_chat_messages(limit, before_message_id, None)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
error!("Failed to load previous chat messages: {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(ChatMessageListPB {
|
|
|
|
messages,
|
2024-06-09 06:02:32 +00:00
|
|
|
has_more: true,
|
2024-06-03 06:27:28 +00:00
|
|
|
total: 0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn load_latest_chat_messages(
|
|
|
|
&self,
|
|
|
|
limit: i64,
|
|
|
|
after_message_id: Option<i64>,
|
|
|
|
) -> Result<ChatMessageListPB, FlowyError> {
|
|
|
|
trace!(
|
2024-06-09 06:02:32 +00:00
|
|
|
"[Chat] Loading new messages: chat_id={}, limit={}, after_message_id={:?}",
|
2024-06-03 06:27:28 +00:00
|
|
|
self.chat_id,
|
|
|
|
limit,
|
|
|
|
after_message_id,
|
|
|
|
);
|
|
|
|
let messages = self
|
|
|
|
.load_local_chat_messages(limit, after_message_id, None)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
trace!(
|
2024-06-09 06:02:32 +00:00
|
|
|
"[Chat] Loaded local chat messages: chat_id={}, messages={}",
|
2024-06-03 06:27:28 +00:00
|
|
|
self.chat_id,
|
|
|
|
messages.len()
|
|
|
|
);
|
|
|
|
|
|
|
|
// If the number of messages equals the limit, then no need to load more messages from remote
|
|
|
|
let has_more = !messages.is_empty();
|
|
|
|
let _ = self
|
|
|
|
.load_remote_chat_messages(limit, None, after_message_id)
|
|
|
|
.await;
|
|
|
|
Ok(ChatMessageListPB {
|
|
|
|
messages,
|
|
|
|
has_more,
|
|
|
|
total: 0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn load_remote_chat_messages(
|
|
|
|
&self,
|
|
|
|
limit: i64,
|
|
|
|
before_message_id: Option<i64>,
|
|
|
|
after_message_id: Option<i64>,
|
|
|
|
) -> FlowyResult<()> {
|
|
|
|
trace!(
|
2024-06-09 06:02:32 +00:00
|
|
|
"[Chat] start loading messages from remote: chat_id={}, limit={}, before_message_id={:?}, after_message_id={:?}",
|
2024-06-03 06:27:28 +00:00
|
|
|
self.chat_id,
|
|
|
|
limit,
|
|
|
|
before_message_id,
|
|
|
|
after_message_id
|
|
|
|
);
|
|
|
|
let workspace_id = self.user_service.workspace_id()?;
|
|
|
|
let chat_id = self.chat_id.clone();
|
2024-06-30 09:38:39 +00:00
|
|
|
let cloud_service = self.chat_service.clone();
|
2024-06-03 06:27:28 +00:00
|
|
|
let user_service = self.user_service.clone();
|
|
|
|
let uid = self.uid;
|
|
|
|
let prev_message_state = self.prev_message_state.clone();
|
|
|
|
let latest_message_id = self.latest_message_id.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
let cursor = match (before_message_id, after_message_id) {
|
|
|
|
(Some(bid), _) => MessageCursor::BeforeMessageId(bid),
|
|
|
|
(_, Some(aid)) => MessageCursor::AfterMessageId(aid),
|
|
|
|
_ => MessageCursor::NextBack,
|
|
|
|
};
|
|
|
|
match cloud_service
|
|
|
|
.get_chat_messages(&workspace_id, &chat_id, cursor.clone(), limit as u64)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(resp) => {
|
|
|
|
// Save chat messages to local disk
|
|
|
|
if let Err(err) = save_chat_message(
|
|
|
|
user_service.sqlite_connection(uid)?,
|
|
|
|
&chat_id,
|
|
|
|
resp.messages.clone(),
|
|
|
|
) {
|
|
|
|
error!("Failed to save chat:{} messages: {}", chat_id, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update latest message ID
|
|
|
|
if !resp.messages.is_empty() {
|
|
|
|
latest_message_id.store(
|
|
|
|
resp.messages[0].message_id,
|
|
|
|
std::sync::atomic::Ordering::Relaxed,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let pb = ChatMessageListPB::from(resp);
|
|
|
|
trace!(
|
2024-06-09 06:02:32 +00:00
|
|
|
"[Chat] Loaded messages from remote: chat_id={}, messages={}, hasMore: {}, cursor:{:?}",
|
2024-06-03 06:27:28 +00:00
|
|
|
chat_id,
|
2024-06-09 06:02:32 +00:00
|
|
|
pb.messages.len(),
|
|
|
|
pb.has_more,
|
|
|
|
cursor,
|
2024-06-03 06:27:28 +00:00
|
|
|
);
|
|
|
|
if matches!(cursor, MessageCursor::BeforeMessageId(_)) {
|
|
|
|
if pb.has_more {
|
|
|
|
*prev_message_state.write().await = PrevMessageState::HasMore;
|
|
|
|
} else {
|
|
|
|
*prev_message_state.write().await = PrevMessageState::NoMore;
|
|
|
|
}
|
2024-07-18 12:54:35 +00:00
|
|
|
make_notification(&chat_id, ChatNotification::DidLoadPrevChatMessage)
|
2024-06-03 06:27:28 +00:00
|
|
|
.payload(pb)
|
|
|
|
.send();
|
|
|
|
} else {
|
2024-07-18 12:54:35 +00:00
|
|
|
make_notification(&chat_id, ChatNotification::DidLoadLatestChatMessage)
|
2024-06-03 06:27:28 +00:00
|
|
|
.payload(pb)
|
|
|
|
.send();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(err) => error!("Failed to load chat messages: {}", err),
|
|
|
|
}
|
|
|
|
Ok::<(), FlowyError>(())
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_related_question(
|
|
|
|
&self,
|
|
|
|
message_id: i64,
|
|
|
|
) -> Result<RepeatedRelatedQuestionPB, FlowyError> {
|
|
|
|
let workspace_id = self.user_service.workspace_id()?;
|
|
|
|
let resp = self
|
2024-06-30 09:38:39 +00:00
|
|
|
.chat_service
|
2024-06-03 06:27:28 +00:00
|
|
|
.get_related_message(&workspace_id, &self.chat_id, message_id)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
trace!(
|
2024-06-09 06:02:32 +00:00
|
|
|
"[Chat] related messages: chat_id={}, message_id={}, messages:{:?}",
|
2024-06-03 06:27:28 +00:00
|
|
|
self.chat_id,
|
|
|
|
message_id,
|
|
|
|
resp.items
|
|
|
|
);
|
|
|
|
Ok(RepeatedRelatedQuestionPB::from(resp))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(level = "debug", skip_all, err)]
|
|
|
|
pub async fn generate_answer(&self, question_message_id: i64) -> FlowyResult<ChatMessagePB> {
|
2024-06-09 06:02:32 +00:00
|
|
|
trace!(
|
|
|
|
"[Chat] generate answer: chat_id={}, question_message_id={}",
|
|
|
|
self.chat_id,
|
|
|
|
question_message_id
|
|
|
|
);
|
2024-06-03 06:27:28 +00:00
|
|
|
let workspace_id = self.user_service.workspace_id()?;
|
2024-06-09 06:02:32 +00:00
|
|
|
let answer = self
|
2024-06-30 09:38:39 +00:00
|
|
|
.chat_service
|
2024-06-03 06:27:28 +00:00
|
|
|
.generate_answer(&workspace_id, &self.chat_id, question_message_id)
|
|
|
|
.await?;
|
|
|
|
|
2024-06-09 06:02:32 +00:00
|
|
|
Self::save_answer(self.uid, &self.chat_id, &self.user_service, answer.clone())?;
|
|
|
|
let pb = ChatMessagePB::from(answer);
|
2024-06-03 06:27:28 +00:00
|
|
|
Ok(pb)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn load_local_chat_messages(
|
|
|
|
&self,
|
|
|
|
limit: i64,
|
|
|
|
after_message_id: Option<i64>,
|
|
|
|
before_message_id: Option<i64>,
|
|
|
|
) -> Result<Vec<ChatMessagePB>, FlowyError> {
|
|
|
|
let conn = self.user_service.sqlite_connection(self.uid)?;
|
|
|
|
let records = select_chat_messages(
|
|
|
|
conn,
|
|
|
|
&self.chat_id,
|
|
|
|
limit,
|
|
|
|
after_message_id,
|
|
|
|
before_message_id,
|
|
|
|
)?;
|
|
|
|
let messages = records
|
|
|
|
.into_iter()
|
|
|
|
.map(|record| ChatMessagePB {
|
|
|
|
message_id: record.message_id,
|
|
|
|
content: record.content,
|
|
|
|
created_at: record.created_at,
|
|
|
|
author_type: record.author_type,
|
|
|
|
author_id: record.author_id,
|
|
|
|
reply_message_id: record.reply_message_id,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
Ok(messages)
|
|
|
|
}
|
2024-07-15 07:23:23 +00:00
|
|
|
|
|
|
|
#[instrument(level = "debug", skip_all, err)]
|
|
|
|
pub async fn index_file(&self, file_path: PathBuf) -> FlowyResult<()> {
|
|
|
|
if !file_path.exists() {
|
|
|
|
return Err(
|
|
|
|
FlowyError::record_not_found().with_context(format!("{:?} not exist", file_path)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !file_path.is_file() {
|
|
|
|
return Err(
|
|
|
|
FlowyError::invalid_data().with_context(format!("{:?} is not a file ", file_path)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
trace!(
|
|
|
|
"[Chat] index file: chat_id={}, file_path={:?}",
|
|
|
|
self.chat_id,
|
|
|
|
file_path
|
|
|
|
);
|
|
|
|
self
|
|
|
|
.chat_service
|
|
|
|
.index_file(&self.user_service.workspace_id()?, file_path, &self.chat_id)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-06-03 06:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn save_chat_message(
|
|
|
|
conn: DBConnection,
|
|
|
|
chat_id: &str,
|
|
|
|
messages: Vec<ChatMessage>,
|
|
|
|
) -> FlowyResult<()> {
|
|
|
|
let records = messages
|
|
|
|
.into_iter()
|
|
|
|
.map(|message| ChatMessageTable {
|
|
|
|
message_id: message.message_id,
|
|
|
|
chat_id: chat_id.to_string(),
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
insert_chat_messages(conn, &records)?;
|
|
|
|
Ok(())
|
|
|
|
}
|