From 6779a13a29a61c045810eb7257bbc0c89d8a72c6 Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (Pragmatismo)" Date: Sun, 12 Oct 2025 13:27:48 -0300 Subject: [PATCH] - Set mode is now a number. --- src/auth/mod.rs | 32 +++++++++++++++++++++++++------- src/automation/mod.rs | 2 +- src/bot/mod.rs | 12 +++++++----- src/session/mod.rs | 4 ++-- src/shared/models.rs | 15 +++++++++++++-- 5 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/auth/mod.rs b/src/auth/mod.rs index f59ad7ff7..14afb8ea1 100644 --- a/src/auth/mod.rs +++ b/src/auth/mod.rs @@ -2,12 +2,14 @@ use argon2::{ password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString}, Argon2, }; -use diesel::prelude::*; use diesel::pg::PgConnection; +use diesel::prelude::*; use redis::Client; use std::sync::Arc; use uuid::Uuid; +use crate::shared; + pub struct AuthService { pub conn: PgConnection, pub redis: Option>, @@ -24,7 +26,7 @@ impl AuthService { password: &str, ) -> Result, Box> { use crate::shared::models::users; - + let user = users::table .filter(users::username.eq(username)) .filter(users::is_active.eq(true)) @@ -54,15 +56,16 @@ impl AuthService { ) -> Result> { use crate::shared::models::users; use diesel::insert_into; - + let salt = SaltString::generate(&mut OsRng); let argon2 = Argon2::default(); - let password_hash = argon2.hash_password(password.as_bytes(), &salt) + let password_hash = argon2 + .hash_password(password.as_bytes(), &salt) .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))? .to_string(); let user_id = Uuid::new_v4(); - + insert_into(users::table) .values(( users::id.eq(user_id), @@ -95,10 +98,11 @@ impl AuthService { ) -> Result<(), Box> { use crate::shared::models::users; use diesel::update; - + let salt = SaltString::generate(&mut OsRng); let argon2 = Argon2::default(); - let password_hash = argon2.hash_password(new_password.as_bytes(), &salt) + let password_hash = argon2 + .hash_password(new_password.as_bytes(), &salt) .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))? .to_string(); @@ -122,4 +126,18 @@ impl AuthService { Ok(()) } + pub(crate) fn get_user_by_id( + &mut self, + uid: Uuid, + ) -> Result, Box> { + use crate::shared::models::users; + + let user = users::table + .filter(users::id.eq(uid)) + .filter(users::is_active.eq(true)) + .first::(&mut self.conn) + .optional()?; + + Ok(user) + } } diff --git a/src/automation/mod.rs b/src/automation/mod.rs index 1db2a35fa..483cb40c2 100644 --- a/src/automation/mod.rs +++ b/src/automation/mod.rs @@ -208,7 +208,7 @@ impl AutomationService { user_id: Uuid::new_v4(), bot_id: Uuid::new_v4(), title: "Automation".to_string(), - answer_mode: "direct".to_string(), + answer_mode: 0, current_tool: None, context_data: serde_json::Value::Null, created_at: Utc::now(), diff --git a/src/bot/mod.rs b/src/bot/mod.rs index 441abe6ee..5dc9bc394 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -76,7 +76,7 @@ impl BotOrchestrator { &self, user_id: &str, bot_id: &str, - mode: &str, + mode: i32, ) -> Result<(), Box> { let mut session_manager = self.session_manager.lock().await; session_manager.update_answer_mode(user_id, bot_id, mode)?; @@ -130,7 +130,7 @@ impl BotOrchestrator { } } - if session.answer_mode == "tool" && session.current_tool.is_some() { + if session.answer_mode == 1 && session.current_tool.is_some() { self.tool_manager.provide_user_response( &message.user_id, &message.bot_id, @@ -226,7 +226,7 @@ impl BotOrchestrator { }; // If the session is awaiting tool input, forward the user's answer to the tool manager. - if session.answer_mode == "tool" && session.current_tool.is_some() { + if session.answer_mode == 1 && session.current_tool.is_some() { self.tool_manager.provide_user_response( &message.user_id, &message.bot_id, @@ -666,11 +666,13 @@ async fn set_mode_handler( ) -> Result { let default_user = "default_user".to_string(); let default_bot = "default_bot".to_string(); - let default_mode = "direct".to_string(); + let default_mode = "0".to_string(); let user_id = info.get("user_id").unwrap_or(&default_user); let bot_id = info.get("bot_id").unwrap_or(&default_bot); - let mode = info.get("mode").unwrap_or(&default_mode); + let mode_str = info.get("mode").unwrap_or(&default_mode); + + let mode = mode_str.parse::().unwrap_or(0); if let Err(e) = data .orchestrator diff --git a/src/session/mod.rs b/src/session/mod.rs index 64ffda5d3..c5239cd9f 100644 --- a/src/session/mod.rs +++ b/src/session/mod.rs @@ -115,7 +115,7 @@ impl SessionManager { bot_id.eq(bid), title.eq(session_title), context_data.eq(serde_json::json!({})), - answer_mode.eq("direct"), + answer_mode.eq(0), current_tool.eq(None::), created_at.eq(now), updated_at.eq(now), @@ -191,7 +191,7 @@ impl SessionManager { &mut self, uid: &str, bid: &str, - mode: &str, + mode: i32, ) -> Result<(), Box> { use crate::shared::models::user_sessions::dsl::*; diff --git a/src/shared/models.rs b/src/shared/models.rs index 2c2a7ced0..0781d35eb 100644 --- a/src/shared/models.rs +++ b/src/shared/models.rs @@ -11,6 +11,17 @@ pub struct Organization { pub slug: String, pub created_at: chrono::DateTime, } +#[derive(Debug, Clone, Queryable, Serialize, Deserialize)] +#[diesel(table_name = users)] +pub struct User { + pub id: Uuid, + pub username: String, + pub email: String, + pub password_hash: String, + pub is_active: bool, + pub created_at: chrono::DateTime, + pub updated_at: chrono::DateTime, +} #[derive(Debug, Clone, Serialize, Deserialize, Queryable)] #[diesel(table_name = bots)] @@ -70,7 +81,7 @@ pub struct UserSession { pub bot_id: Uuid, pub title: String, pub context_data: serde_json::Value, - pub answer_mode: String, + pub answer_mode: i32, pub current_tool: Option, pub created_at: chrono::DateTime, pub updated_at: chrono::DateTime, @@ -165,7 +176,7 @@ diesel::table! { bot_id -> Uuid, title -> Text, context_data -> Jsonb, - answer_mode -> Text, + answer_mode -> Int4, current_tool -> Nullable, created_at -> Timestamptz, updated_at -> Timestamptz,