From 9884d93aa2805013f36a79c1757174a0b5063065 Mon Sep 17 00:00:00 2001 From: "Nathan.fooo" <86001920+appflowy@users.noreply.github.com> Date: Fri, 28 Jun 2024 13:43:58 +0800 Subject: [PATCH] chore: rename function and add docs (#663) --- libs/client-api/src/http_chat.rs | 62 +++++++++++++++++++------------- src/api/chat.rs | 1 + tests/ai_test/chat_test.rs | 12 +++---- 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/libs/client-api/src/http_chat.rs b/libs/client-api/src/http_chat.rs index 2821547f..da7e1998 100644 --- a/libs/client-api/src/http_chat.rs +++ b/libs/client-api/src/http_chat.rs @@ -11,6 +11,7 @@ use shared_entity::dto::ai_dto::RepeatedRelatedQuestion; use shared_entity::response::{AppResponse, AppResponseError}; impl Client { + /// Create a new chat pub async fn create_chat( &self, workspace_id: &str, @@ -26,6 +27,8 @@ impl Client { log_request_id(&resp); AppResponse::<()>::from_response(resp).await?.into_error() } + + /// Delete a chat for given chat_id pub async fn delete_chat( &self, workspace_id: &str, @@ -41,27 +44,8 @@ impl Client { AppResponse::<()>::from_response(resp).await?.into_error() } - pub async fn create_chat_qa_message( - &self, - workspace_id: &str, - chat_id: &str, - params: CreateChatMessageParams, - ) -> Result>, AppResponseError> { - let url = format!( - "{}/api/chat/{workspace_id}/{chat_id}/message", - self.base_url - ); - let resp = self - .http_client_with_auth(Method::POST, &url) - .await? - .json(¶ms) - .send() - .await?; - log_request_id(&resp); - AppResponse::::json_response_stream(resp).await - } - - pub async fn create_question( + /// Save a question message to a chat + pub async fn save_question( &self, workspace_id: &str, chat_id: &str, @@ -83,7 +67,8 @@ impl Client { .into_data() } - pub async fn create_answer( + /// save an answer message to a chat + pub async fn save_answer( &self, workspace_id: &str, chat_id: &str, @@ -105,7 +90,8 @@ impl Client { .into_data() } - pub async fn stream_answer( + /// Ask AI with a question for given question's message_id + pub async fn ask_question( &self, workspace_id: &str, chat_id: &str, @@ -124,8 +110,9 @@ impl Client { AppResponse::<()>::answer_response_stream(resp).await } - /// Returns the answer to a question message. - pub async fn get_answer( + /// Generate an answer for given question's message_id. The same as ask_question but return ChatMessage + /// instead of stream of Bytes + pub async fn generate_answer( &self, workspace_id: &str, chat_id: &str, @@ -146,6 +133,8 @@ impl Client { .into_data() } + /// Update chat message content. It will override the content of the message. + /// A message can be a question or an answer pub async fn update_chat_message( &self, workspace_id: &str, @@ -166,6 +155,7 @@ impl Client { AppResponse::<()>::from_response(resp).await?.into_error() } + /// Get related question for a chat message. The message_d should be the question's id pub async fn get_chat_related_question( &self, workspace_id: &str, @@ -187,6 +177,7 @@ impl Client { .into_data() } + /// Return list of chat messages for a chat pub async fn get_chat_messages( &self, workspace_id: &str, @@ -219,4 +210,25 @@ impl Client { .await? .into_data() } + + /// It's no longer used in the frontend application since 0.6.0 version. + pub async fn create_question_answer( + &self, + workspace_id: &str, + chat_id: &str, + params: CreateChatMessageParams, + ) -> Result>, AppResponseError> { + let url = format!( + "{}/api/chat/{workspace_id}/{chat_id}/message", + self.base_url + ); + let resp = self + .http_client_with_auth(Method::POST, &url) + .await? + .json(¶ms) + .send() + .await?; + log_request_id(&resp); + AppResponse::::json_response_stream(resp).await + } } diff --git a/src/api/chat.rs b/src/api/chat.rs index f5abde73..880c0e06 100644 --- a/src/api/chat.rs +++ b/src/api/chat.rs @@ -46,6 +46,7 @@ pub fn chat_scope() -> Scope { ) .service( web::resource("/{chat_id}/message") + // create_chat_message_handler is deprecated. No long used after frontend application v0.6.2 .route(web::post().to(create_chat_message_handler)) .route(web::put().to(update_chat_message_handler)), ) diff --git a/tests/ai_test/chat_test.rs b/tests/ai_test/chat_test.rs index d659fd4a..cf771f43 100644 --- a/tests/ai_test/chat_test.rs +++ b/tests/ai_test/chat_test.rs @@ -25,7 +25,7 @@ async fn create_chat_and_create_messages_test() { let params = CreateChatMessageParams::new_system(format!("hello world {}", i)); let message = test_client .api_client - .create_chat_qa_message(&workspace_id, &chat_id, params) + .create_question_answer(&workspace_id, &chat_id, params) .await .unwrap() .next() @@ -111,7 +111,7 @@ async fn chat_qa_test() { let params = CreateChatMessageParams::new_user("where is singapore?"); let stream = test_client .api_client - .create_chat_qa_message(&workspace_id, &chat_id, params) + .create_question_answer(&workspace_id, &chat_id, params) .await .unwrap(); @@ -146,7 +146,7 @@ async fn generate_chat_message_answer_test() { let params = CreateChatMessageParams::new_user("where is singapore?"); let stream = test_client .api_client - .create_chat_qa_message(&workspace_id, &chat_id, params) + .create_question_answer(&workspace_id, &chat_id, params) .await .unwrap(); let messages: Vec = stream.map(|message| message.unwrap()).collect().await; @@ -154,7 +154,7 @@ async fn generate_chat_message_answer_test() { let answer = test_client .api_client - .get_answer(&workspace_id, &chat_id, messages[0].message_id) + .generate_answer(&workspace_id, &chat_id, messages[0].message_id) .await .unwrap(); @@ -188,13 +188,13 @@ async fn generate_stream_answer_test() { let params = CreateChatMessageParams::new_user("Teach me how to write a article?"); let question = test_client .api_client - .create_question(&workspace_id, &chat_id, params) + .save_question(&workspace_id, &chat_id, params) .await .unwrap(); let mut answer_stream = test_client .api_client - .stream_answer(&workspace_id, &chat_id, question.message_id) + .ask_question(&workspace_id, &chat_id, question.message_id) .await .unwrap();