- Set mode is now a number.

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-10-12 13:27:48 -03:00
parent fa9f163971
commit 6779a13a29
5 changed files with 48 additions and 17 deletions

View file

@ -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<Arc<Client>>,
@ -24,7 +26,7 @@ impl AuthService {
password: &str,
) -> Result<Option<Uuid>, Box<dyn std::error::Error + Send + Sync>> {
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<Uuid, Box<dyn std::error::Error + Send + Sync>> {
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<dyn std::error::Error + Send + Sync>> {
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<Option<shared::models::User>, Box<dyn std::error::Error + Send + Sync>> {
use crate::shared::models::users;
let user = users::table
.filter(users::id.eq(uid))
.filter(users::is_active.eq(true))
.first::<shared::models::User>(&mut self.conn)
.optional()?;
Ok(user)
}
}

View file

@ -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(),

View file

@ -76,7 +76,7 @@ impl BotOrchestrator {
&self,
user_id: &str,
bot_id: &str,
mode: &str,
mode: i32,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
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<HttpResponse> {
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::<i32>().unwrap_or(0);
if let Err(e) = data
.orchestrator

View file

@ -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::<String>),
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<dyn Error + Send + Sync>> {
use crate::shared::models::user_sessions::dsl::*;

View file

@ -11,6 +11,17 @@ pub struct Organization {
pub slug: String,
pub created_at: chrono::DateTime<chrono::Utc>,
}
#[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<chrono::Utc>,
pub updated_at: chrono::DateTime<chrono::Utc>,
}
#[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<String>,
pub created_at: chrono::DateTime<Utc>,
pub updated_at: chrono::DateTime<Utc>,
@ -165,7 +176,7 @@ diesel::table! {
bot_id -> Uuid,
title -> Text,
context_data -> Jsonb,
answer_mode -> Text,
answer_mode -> Int4,
current_tool -> Nullable<Text>,
created_at -> Timestamptz,
updated_at -> Timestamptz,