diff --git a/Cargo.toml b/Cargo.toml index 2a5955ec..502c7ac1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ features = ["database", "i18n"] [features] # ===== DEFAULT ===== -default = ["chat", "people", "automation", "drive", "tasks", "cache", "directory", "llm", "crawler", "browser", "terminal", "editor", "mail", "whatsapp", "designer", "marketing", "goals", "analytics"] +default = ["chat", "people", "automation", "drive", "tasks", "cache", "directory", "llm", "crawler", "browser", "terminal", "editor", "mail", "whatsapp", "designer", "marketing", "goals", "analytics", "vectordb", "research"] browser = ["automation", "drive", "cache"] terminal = ["automation", "drive", "cache"] diff --git a/src/basic/keywords/think_kb.rs b/src/basic/keywords/think_kb.rs index fc8290c0..9fc868a5 100644 --- a/src/basic/keywords/think_kb.rs +++ b/src/basic/keywords/think_kb.rs @@ -13,6 +13,7 @@ //! - confidence: Overall confidence score (0.0 to 1.0) use crate::core::bot::kb_context::KbContextManager; +use diesel::prelude::*; use crate::core::kb::KnowledgeBaseManager; use crate::core::shared::models::UserSession; use crate::core::shared::state::AppState; @@ -40,7 +41,6 @@ pub fn register_think_kb_keyword( let session_id = session_clone.id; let bot_id = session_clone.bot_id; - let bot_name = session_clone.bot_name.clone(); let kb_manager = match &state_clone.kb_manager { Some(manager) => Arc::clone(manager), None => { @@ -53,7 +53,7 @@ pub fn register_think_kb_keyword( // Execute KB search in blocking thread let result = std::thread::spawn(move || { tokio::runtime::Handle::current().block_on(async { - think_kb_search(kb_manager, db_pool, session_id, bot_id, &bot_name, &query).await + think_kb_search(kb_manager, db_pool, session_id, bot_id, &query).await }) }) .join(); @@ -94,14 +94,23 @@ async fn think_kb_search( db_pool: crate::core::shared::utils::DbPool, session_id: uuid::Uuid, bot_id: uuid::Uuid, - bot_name: &str, query: &str, ) -> Result { + use crate::core::shared::models::schema::bots; + + let bot_name = { + let mut conn = db_pool.get().map_err(|e| format!("DB error: {}", e))?; + diesel::QueryDsl::filter(bots::table, bots::id.eq(bot_id)) + .select(bots::name) + .first::(&mut *conn) + .map_err(|e| format!("Failed to get bot name for id {}: {}", bot_id, e))? + }; + let context_manager = KbContextManager::new(kb_manager, db_pool); // Search active KBs with reasonable limits let kb_contexts = context_manager - .search_active_kbs(session_id, bot_id, bot_name, query, 10, 2000) + .search_active_kbs(session_id, bot_id, &bot_name, query, 10, 2000) .await .map_err(|e| format!("KB search failed: {}", e))?; diff --git a/src/basic/keywords/use_kb.rs b/src/basic/keywords/use_kb.rs index 78260ca6..cdb3dee3 100644 --- a/src/basic/keywords/use_kb.rs +++ b/src/basic/keywords/use_kb.rs @@ -38,17 +38,20 @@ pub fn register_use_kb_keyword( let state_clone = Arc::clone(&state); let session_clone = Arc::clone(&session); + let session_clone_for_syntax = session_clone.clone(); + let state_clone_for_syntax = state_clone.clone(); + engine.register_custom_syntax(["USE", "KB", "$expr$"], true, move |context, inputs| { let kb_name = context.eval_expression_tree(&inputs[0])?.to_string(); info!( "USE KB keyword executed - KB: {}, Session: {}", - kb_name, session_clone.id + kb_name, session_clone_for_syntax.id ); - let session_id = session_clone.id; - let bot_id = session_clone.bot_id; - let conn = state_clone.conn.clone(); + let session_id = session_clone_for_syntax.id; + let bot_id = session_clone_for_syntax.bot_id; + let conn = state_clone_for_syntax.conn.clone(); let kb_name_clone = kb_name.clone(); let result = @@ -57,7 +60,10 @@ pub fn register_use_kb_keyword( match result { Ok(Ok(_)) => { - info!(" KB '{}' added to session {}", kb_name, session_clone.id); + info!( + " KB '{}' added to session {}", + kb_name, session_clone_for_syntax.id + ); Ok(Dynamic::UNIT) } Ok(Err(e)) => { @@ -71,6 +77,79 @@ pub fn register_use_kb_keyword( } })?; + let session_clone2 = session_clone.clone(); + let state_clone2 = state_clone.clone(); + + info!( + "Registering USE_KB function for session: {}", + session_clone.id + ); + + let session_clone_lower = session_clone.clone(); + let state_clone_lower = state_clone.clone(); + + engine.register_fn("use_kb", move |kb_name: &str| -> Dynamic { + info!( + "use_kb function called - KB: {}, Session: {}", + kb_name, session_clone_lower.id + ); + + let session_id = session_clone_lower.id; + let bot_id = session_clone_lower.bot_id; + let conn = state_clone_lower.conn.clone(); + let kb_name_clone = kb_name.to_string(); + + let result = + std::thread::spawn(move || add_kb_to_session(conn, session_id, bot_id, &kb_name_clone)) + .join(); + + match result { + Ok(Ok(_)) => { + info!(" use_kb '{}' added to session {}", kb_name, session_id); + Dynamic::UNIT + } + Ok(Err(e)) => { + error!("Failed to add KB '{}': {}", kb_name, e); + Dynamic::from(format!("USE_KB failed: {}", e)) + } + Err(e) => { + error!("Thread panic in USE_KB: {:?}", e); + Dynamic::from("USE_KB failed: thread panic") + } + } + }); + + engine.register_fn("USE_KB", move |kb_name: &str| -> Dynamic { + info!( + "USE_KB function called - KB: {}, Session: {}", + kb_name, session_clone2.id + ); + + let session_id = session_clone2.id; + let bot_id = session_clone2.bot_id; + let conn = state_clone2.conn.clone(); + let kb_name_clone = kb_name.to_string(); + + let result = + std::thread::spawn(move || add_kb_to_session(conn, session_id, bot_id, &kb_name_clone)) + .join(); + + match result { + Ok(Ok(_)) => { + info!(" USE_KB '{}' added to session {}", kb_name, session_id); + Dynamic::UNIT + } + Ok(Err(e)) => { + error!("Failed to add KB '{}': {}", kb_name, e); + Dynamic::from(format!("USE_KB failed: {}", e)) + } + Err(e) => { + error!("Thread panic in USE_KB: {:?}", e); + Dynamic::from("USE_KB failed: thread panic") + } + } + }); + Ok(()) } diff --git a/src/core/package_manager/installer.rs b/src/core/package_manager/installer.rs index 8e7495bf..d8088ae4 100644 --- a/src/core/package_manager/installer.rs +++ b/src/core/package_manager/installer.rs @@ -1122,6 +1122,27 @@ EOF"#.to_string(), .map_err(|e| anyhow::anyhow!("Failed to create noop process: {}", e)); } + // Generate qdrant config.yaml if missing + if component.name == "vector_db" { + let qdrant_conf = conf_path.join("vector_db/config.yaml"); + if !qdrant_conf.exists() { + let storage = data_path.join("storage"); + let snapshots = data_path.join("snapshots"); + let _ = std::fs::create_dir_all(&storage); + let _ = std::fs::create_dir_all(&snapshots); + let yaml = format!( + "storage:\n storage_path: {}\n snapshots_path: {}\n\nservice:\n host: 0.0.0.0\n http_port: 6333\n grpc_port: 6334\n enable_tls: false\n\nlog_level: INFO\n", + storage.display(), + snapshots.display() + ); + if let Err(e) = std::fs::write(&qdrant_conf, yaml) { + warn!("Failed to write qdrant config: {}", e); + } else { + info!("Generated qdrant config at {}", qdrant_conf.display()); + } + } + } + let rendered_cmd = component .exec_cmd .replace("{{BIN_PATH}}", &bin_path.to_string_lossy())