Skip to content

Commit

Permalink
fix: add author uuid to response for create AI chat question
Browse files Browse the repository at this point in the history
  • Loading branch information
khorshuheng committed Mar 4, 2025
1 parent 3e1d5b0 commit a392815
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
7 changes: 4 additions & 3 deletions libs/database/src/chat/chat_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use tracing::warn;

use uuid::Uuid;

//noinspection ALL
pub async fn insert_chat<'a, E: Executor<'a, Database = Postgres>>(
executor: E,
workspace_id: &str,
Expand Down Expand Up @@ -329,11 +330,11 @@ pub async fn insert_answer_message(

pub async fn insert_question_message<'a, E: Executor<'a, Database = Postgres>>(
executor: E,
author: ChatAuthor,
author: ChatAuthorWithUuid,
chat_id: &str,
content: String,
metadata: Vec<ChatMessageMetadata>,
) -> Result<ChatMessage, AppError> {
) -> Result<ChatMessageWithAuthorUuid, AppError> {
let metadata = json!(metadata);
let chat_id = Uuid::from_str(chat_id)?;
let row = sqlx::query!(
Expand All @@ -351,7 +352,7 @@ pub async fn insert_question_message<'a, E: Executor<'a, Database = Postgres>>(
.await
.map_err(|err| AppError::Internal(anyhow!("Failed to insert chat message: {}", err)))?;

let chat_message = ChatMessage {
let chat_message = ChatMessageWithAuthorUuid {
author,
message_id: row.message_id,
content,
Expand Down
11 changes: 6 additions & 5 deletions src/api/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ use futures_util::stream;
use futures_util::{FutureExt, TryStreamExt};
use pin_project::pin_project;
use shared_entity::dto::chat_dto::{
ChatAuthor, ChatMessage, ChatSettings, CreateAnswerMessageParams, CreateChatMessageParams,
CreateChatMessageParamsV2, CreateChatParams, GetChatMessageParams, MessageCursor,
RepeatedChatMessageWithAuthorUuid, UpdateChatMessageContentParams, UpdateChatParams,
ChatAuthor, ChatMessage, ChatMessageWithAuthorUuid, ChatSettings, CreateAnswerMessageParams,
CreateChatMessageParams, CreateChatMessageParamsV2, CreateChatParams, GetChatMessageParams,
MessageCursor, RepeatedChatMessageWithAuthorUuid, UpdateChatMessageContentParams,
UpdateChatParams,
};
use shared_entity::response::{AppResponse, JsonAppResponse};
use std::collections::HashMap;
Expand Down Expand Up @@ -181,7 +182,7 @@ async fn create_question_handler(
path: web::Path<(String, String)>,
payload: Json<CreateChatMessageParams>,
uuid: UserUuid,
) -> actix_web::Result<JsonAppResponse<ChatMessage>> {
) -> actix_web::Result<JsonAppResponse<ChatMessageWithAuthorUuid>> {
let (_workspace_id, chat_id) = path.into_inner();
let params = payload.into_inner();

Expand All @@ -206,7 +207,7 @@ async fn create_question_handler(
}

let uid = state.user_cache.get_user_uid(&uuid).await?;
let resp = create_chat_message(&state.pg_pool, uid, chat_id, params).await?;
let resp = create_chat_message(&state.pg_pool, uid, *uuid, chat_id, params).await?;
Ok(AppResponse::Ok().with_data(resp).into())
}

Expand Down
16 changes: 10 additions & 6 deletions src/biz/chat/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ use database::chat::chat_ops::{
use futures::stream::Stream;
use serde_json::json;
use shared_entity::dto::chat_dto::{
ChatAuthor, ChatAuthorType, ChatMessage, ChatMessageType, CreateChatMessageParams,
CreateChatParams, GetChatMessageParams, RepeatedChatMessage, RepeatedChatMessageWithAuthorUuid,
UpdateChatMessageContentParams,
ChatAuthor, ChatAuthorType, ChatAuthorWithUuid, ChatMessage, ChatMessageType,
ChatMessageWithAuthorUuid, CreateChatMessageParams, CreateChatParams, GetChatMessageParams,
RepeatedChatMessage, RepeatedChatMessageWithAuthorUuid, UpdateChatMessageContentParams,
};
use sqlx::PgPool;
use tracing::{error, info, trace};

use uuid::Uuid;
use validator::Validate;

pub(crate) async fn create_chat(
Expand Down Expand Up @@ -130,15 +131,16 @@ pub async fn generate_chat_message_answer(
pub async fn create_chat_message(
pg_pool: &PgPool,
uid: i64,
user_uuid: Uuid,
chat_id: String,
params: CreateChatMessageParams,
) -> Result<ChatMessage, AppError> {
) -> Result<ChatMessageWithAuthorUuid, AppError> {
let chat_id = chat_id.clone();
let pg_pool = pg_pool.clone();

let question = insert_question_message(
&pg_pool,
ChatAuthor::new(uid, ChatAuthorType::Human),
ChatAuthorWithUuid::new(uid, user_uuid, ChatAuthorType::Human),
&chat_id,
params.content,
params.metadata,
Expand All @@ -147,9 +149,11 @@ pub async fn create_chat_message(
Ok(question)
}

#[allow(clippy::too_many_arguments)]
pub async fn create_chat_message_stream(
pg_pool: &PgPool,
uid: i64,
user_uuid: Uuid,
workspace_id: String,
chat_id: String,
params: CreateChatMessageParams,
Expand All @@ -164,7 +168,7 @@ pub async fn create_chat_message_stream(
// Insert question message
let question = match insert_question_message(
&pg_pool,
ChatAuthor::new(uid, ChatAuthorType::Human),
ChatAuthorWithUuid::new(uid, user_uuid, ChatAuthorType::Human),
&chat_id,
params.content.clone(),
params.metadata.clone(),
Expand Down
7 changes: 5 additions & 2 deletions tests/sql_test/chat_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use database::chat::chat_ops::{
};
use serde_json::json;
use shared_entity::dto::chat_dto::{
ChatAuthor, ChatAuthorType, CreateChatParams, GetChatMessageParams,
ChatAuthorType, ChatAuthorWithUuid, CreateChatParams, GetChatMessageParams,
};

use shared_entity::dto::chat_dto::UpdateChatParams;
use sqlx::PgPool;
use uuid::Uuid;

#[sqlx::test(migrations = false)]
async fn chat_crud_test(pool: PgPool) {
Expand Down Expand Up @@ -90,10 +91,12 @@ async fn chat_message_crud_test(pool: PgPool) {
}

// create chat messages
let uid = 0;
let user_uuid = Uuid::new_v4();
for i in 0..5 {
let _ = insert_question_message(
&pool,
ChatAuthor::new(0, ChatAuthorType::System),
ChatAuthorWithUuid::new(uid, user_uuid, ChatAuthorType::System),
&chat_id,
format!("message {}", i),
vec![],
Expand Down

0 comments on commit a392815

Please sign in to comment.