From 1765b20917fd1ec543a219f97e68aff590aa2ab4 Mon Sep 17 00:00:00 2001 From: dal Date: Fri, 14 Feb 2025 15:01:38 -0700 Subject: [PATCH] handler format looking good --- .../operations => messages/helpers}/mod.rs | 0 api/libs/handlers/src/messages/mod.rs | 2 + api/libs/handlers/src/messages/types.rs | 19 +++++++++ api/libs/handlers/src/threads/helpers/mod.rs | 0 api/libs/handlers/src/threads/mod.rs | 5 +-- .../handlers/src/threads/operations/create.rs | 37 ----------------- api/libs/handlers/src/threads/types.rs | 25 +++++------ .../post_thread/agent_thread.rs | 41 +++---------------- 8 files changed, 39 insertions(+), 90 deletions(-) rename api/libs/handlers/src/{threads/operations => messages/helpers}/mod.rs (100%) create mode 100644 api/libs/handlers/src/messages/types.rs create mode 100644 api/libs/handlers/src/threads/helpers/mod.rs delete mode 100644 api/libs/handlers/src/threads/operations/create.rs diff --git a/api/libs/handlers/src/threads/operations/mod.rs b/api/libs/handlers/src/messages/helpers/mod.rs similarity index 100% rename from api/libs/handlers/src/threads/operations/mod.rs rename to api/libs/handlers/src/messages/helpers/mod.rs diff --git a/api/libs/handlers/src/messages/mod.rs b/api/libs/handlers/src/messages/mod.rs index e69de29bb..5b5f982c4 100644 --- a/api/libs/handlers/src/messages/mod.rs +++ b/api/libs/handlers/src/messages/mod.rs @@ -0,0 +1,2 @@ +pub mod types; +pub mod helpers; \ No newline at end of file diff --git a/api/libs/handlers/src/messages/types.rs b/api/libs/handlers/src/messages/types.rs new file mode 100644 index 000000000..021bbc581 --- /dev/null +++ b/api/libs/handlers/src/messages/types.rs @@ -0,0 +1,19 @@ +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +#[derive(Debug, Serialize, Deserialize)] +pub struct ThreadMessage { + pub id: Uuid, + pub request_message: ThreadUserMessage, + pub response_messages: Vec, + pub reasoning: Vec, + pub created_at: String, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct ThreadUserMessage { + pub request: String, + pub sender_id: Uuid, + pub sender_name: String, + pub sender_avatar: Option, +} \ No newline at end of file diff --git a/api/libs/handlers/src/threads/helpers/mod.rs b/api/libs/handlers/src/threads/helpers/mod.rs new file mode 100644 index 000000000..e69de29bb diff --git a/api/libs/handlers/src/threads/mod.rs b/api/libs/handlers/src/threads/mod.rs index 1460f6c4f..96c1cb9df 100644 --- a/api/libs/handlers/src/threads/mod.rs +++ b/api/libs/handlers/src/threads/mod.rs @@ -1,5 +1,2 @@ -pub mod operations; pub mod types; - -// Re-export commonly used items -pub use types::{ThreadRequest, ThreadResponse}; +pub mod helpers; diff --git a/api/libs/handlers/src/threads/operations/create.rs b/api/libs/handlers/src/threads/operations/create.rs deleted file mode 100644 index e63d2f625..000000000 --- a/api/libs/handlers/src/threads/operations/create.rs +++ /dev/null @@ -1,37 +0,0 @@ -use anyhow::Result; -use uuid::Uuid; - -use crate::threads::types::{ThreadRequest, ThreadResponse}; - -/// Creates a new thread based on the provided request -pub async fn create_thread(request: ThreadRequest) -> Result { - // This is a placeholder implementation - // You'll need to implement the actual database operations - let now = chrono::Utc::now(); - - Ok(ThreadResponse { - id: Uuid::new_v4(), - title: request.title, - organization_id: request.organization_id, - created_by: request.created_by, - created_at: now, - updated_at: now, - }) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[tokio::test] - async fn test_create_thread() { - let request = ThreadRequest { - title: "Test Thread".to_string(), - organization_id: Uuid::new_v4(), - created_by: Uuid::new_v4(), - }; - - let result = create_thread(request).await; - assert!(result.is_ok()); - } -} \ No newline at end of file diff --git a/api/libs/handlers/src/threads/types.rs b/api/libs/handlers/src/threads/types.rs index 274228e4e..e6d75320c 100644 --- a/api/libs/handlers/src/threads/types.rs +++ b/api/libs/handlers/src/threads/types.rs @@ -1,22 +1,19 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; -use chrono::{DateTime, Utc}; -#[derive(Debug, Serialize, Deserialize)] -pub struct ThreadRequest { - pub title: String, - pub organization_id: Uuid, - pub created_by: Uuid, -} +use crate::messages::types::ThreadMessage; +// Add more thread-related types as needed #[derive(Debug, Serialize, Deserialize)] -pub struct ThreadResponse { +pub struct ThreadWithMessages { pub id: Uuid, pub title: String, - pub organization_id: Uuid, - pub created_by: Uuid, - pub created_at: DateTime, - pub updated_at: DateTime, + pub is_favorited: bool, + pub messages: Vec, + pub created_at: String, + pub updated_at: String, + pub created_by: String, + pub created_by_id: String, + pub created_by_name: String, + pub created_by_avatar: Option, } - -// Add more thread-related types as needed \ No newline at end of file diff --git a/api/src/routes/ws/threads_and_messages/post_thread/agent_thread.rs b/api/src/routes/ws/threads_and_messages/post_thread/agent_thread.rs index bdbfd8786..8669727a2 100644 --- a/api/src/routes/ws/threads_and_messages/post_thread/agent_thread.rs +++ b/api/src/routes/ws/threads_and_messages/post_thread/agent_thread.rs @@ -1,7 +1,9 @@ use anyhow::{Error, Result}; use chrono::Utc; use diesel::{insert_into, ExpressionMethods, QueryDsl}; -use diesel_async::{AsyncConnection, RunQueryDsl}; +use diesel_async::{RunQueryDsl}; +use handlers::messages::types::{ThreadMessage, ThreadUserMessage}; +use handlers::threads::types::ThreadWithMessages; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::collections::HashMap; @@ -43,37 +45,6 @@ use crate::{ use super::agent_message_transformer::transform_message; use litellm::Message as AgentMessage; -#[derive(Debug, Serialize, Deserialize)] -pub struct TempInitChat { - pub id: Uuid, - pub title: String, - pub is_favorited: bool, - pub messages: Vec, - pub created_at: String, - pub updated_at: String, - pub created_by: String, - pub created_by_id: String, - pub created_by_name: String, - pub created_by_avatar: Option, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct TempInitChatMessage { - pub id: Uuid, - pub request_message: TempRequestMessage, - pub response_messages: Vec, - pub reasoning: Vec, - pub created_at: String, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct TempRequestMessage { - pub request: String, - pub sender_id: Uuid, - pub sender_name: String, - pub sender_avatar: Option, -} - #[derive(Debug, Deserialize, Clone)] pub struct ChatCreateNewChat { pub prompt: String, @@ -155,13 +126,13 @@ impl AgentThreadHandler { deleted_at: None, }; - let init_response = TempInitChat { + let init_response = ThreadWithMessages { id: chat_id.clone(), title: request.prompt.clone(), is_favorited: false, - messages: vec![TempInitChatMessage { + messages: vec![ThreadMessage { id: message_id.clone(), - request_message: TempRequestMessage { + request_message: ThreadUserMessage { request: request.prompt.clone(), sender_id: user.id, sender_name: user.name.clone().unwrap_or_default(),