2025-03-03 21:47:18 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use handlers::chats::post_chat_handler::ChatCreateNewChat;
|
2025-03-04 01:21:11 +08:00
|
|
|
use handlers::chats::post_chat_handler::{self, ThreadEvent};
|
2025-03-03 21:47:18 +08:00
|
|
|
use handlers::chats::types::ChatWithMessages;
|
2025-03-03 23:57:30 +08:00
|
|
|
use tokio::sync::mpsc;
|
2025-03-03 21:47:18 +08:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
database::models::User,
|
|
|
|
routes::ws::{
|
2025-03-04 01:21:11 +08:00
|
|
|
threads_and_messages::threads_router::{ThreadEvent as WSThreadEvent, ThreadRoute},
|
2025-03-03 21:47:18 +08:00
|
|
|
ws::{SubscriptionRwLock, WsEvent, WsResponseMessage, WsSendMethod},
|
|
|
|
ws_router::WsRoutes,
|
2025-03-03 23:57:30 +08:00
|
|
|
ws_utils::send_ws_message,
|
2025-03-03 21:47:18 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Creates a new thread for a user and processes their request using the shared handler
|
|
|
|
pub async fn post_thread(
|
|
|
|
subscriptions: &Arc<SubscriptionRwLock>,
|
|
|
|
user_group: &String,
|
|
|
|
user: &User,
|
|
|
|
request: ChatCreateNewChat,
|
|
|
|
) -> Result<()> {
|
2025-03-03 23:57:30 +08:00
|
|
|
let (tx, mut rx) = mpsc::channel(1000);
|
|
|
|
|
2025-03-04 01:21:11 +08:00
|
|
|
let user_id = user.id.to_string();
|
|
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
while let Some(result) = rx.recv().await {
|
|
|
|
match result {
|
|
|
|
Ok((message, event)) => {
|
|
|
|
println!("MESSAGE SHOULD BE SENT: {:?}", message);
|
2025-03-03 21:47:18 +08:00
|
|
|
|
2025-03-04 01:21:11 +08:00
|
|
|
let event = match event {
|
|
|
|
ThreadEvent::GeneratingResponseMessage => {
|
|
|
|
WsEvent::Threads(WSThreadEvent::GeneratingResponseMessage)
|
|
|
|
}
|
|
|
|
ThreadEvent::GeneratingReasoningMessage => {
|
|
|
|
WsEvent::Threads(WSThreadEvent::GeneratingReasoningMessage)
|
|
|
|
}
|
|
|
|
};
|
2025-03-03 23:57:30 +08:00
|
|
|
|
2025-03-04 01:21:11 +08:00
|
|
|
let response = WsResponseMessage::new_no_user(
|
|
|
|
WsRoutes::Threads(ThreadRoute::Post),
|
|
|
|
event,
|
|
|
|
&message,
|
|
|
|
None,
|
|
|
|
WsSendMethod::All,
|
|
|
|
);
|
2025-03-03 23:57:30 +08:00
|
|
|
|
2025-03-04 01:21:11 +08:00
|
|
|
if let Err(e) = send_ws_message(&user_id, &response).await {
|
|
|
|
tracing::error!("Failed to send websocket message: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
tracing::error!("Error in message stream: {:?}", err);
|
|
|
|
return Err(err);
|
2025-03-03 23:57:30 +08:00
|
|
|
}
|
|
|
|
}
|
2025-03-03 21:47:18 +08:00
|
|
|
}
|
2025-03-04 01:21:11 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
// Call the shared handler
|
|
|
|
post_chat_handler::post_chat_handler(request, user.clone(), Some(tx)).await?;
|
2025-03-03 23:57:30 +08:00
|
|
|
|
|
|
|
Ok(())
|
2025-03-03 21:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sends the chat response to the client via WebSocket
|
|
|
|
async fn send_ws_response(subscription: &str, chat_with_messages: &ChatWithMessages) -> Result<()> {
|
|
|
|
let response = WsResponseMessage::new_no_user(
|
|
|
|
WsRoutes::Threads(ThreadRoute::Post),
|
2025-03-04 01:21:11 +08:00
|
|
|
WsEvent::Threads(WSThreadEvent::InitializeChat),
|
2025-03-03 21:47:18 +08:00
|
|
|
chat_with_messages,
|
|
|
|
None,
|
|
|
|
WsSendMethod::All,
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Err(e) = send_ws_message(&subscription.to_string(), &response).await {
|
|
|
|
tracing::error!("Failed to send websocket message: {}", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|