From f2317d16c97e1c91ccacc217f523c152f23eeb65 Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (Pragmatismo)" Date: Wed, 5 Nov 2025 22:48:55 -0300 Subject: [PATCH] feat: update logging config and enable automation modules - Update RUST_LOG configuration in launch.json to include trace level and additional module filters - Uncomment and enable multiple directories in add-req.sh script - Add execute_compact_prompt function to automation module - Extend BasicCompiler comment detection to handle single quotes - Modify BotOrchestrator system message prefix from "SYSTEM" to "SYS" - Add placeholder for compact prompt automation in BotOrchestrator initialization Changes improve debugging capabilities and enable previously commented-out automation features while maintaining existing functionality. --- .vscode/launch.json | 2 +- add-req.sh | 37 ++++++----- src/automation/mod.rs | 24 +++++++ src/basic/compiler/mod.rs | 2 +- src/bot/mod.rs | 64 +++++++++++++++---- .../annoucements.gbot/config.csv | 7 +- 6 files changed, 98 insertions(+), 38 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 0a6f9b47..2a0c72c6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -14,7 +14,7 @@ }, "args": [], "env": { - "RUST_LOG": "debug,reqwest=off,aws_runtime=off,aws_smithy_http_client=off,rustls=off,actix_server=off,hyper_util=off,aws_smithy_runtime=off,aws_smithy_runtime_api=off,tracing=off,aws_sdk_s3=off" + "RUST_LOG": "trace,actix_web=off,aws_sigv4=off,aws_smithy_checksums=off,actix_http=off,mio=off,reqwest=off,aws_runtime=off,aws_smithy_http_client=off,rustls=off,actix_server=off,hyper_util=off,aws_smithy_runtime=off,aws_smithy_runtime_api=off,tracing=off,aws_sdk_s3=off" }, "cwd": "${workspaceFolder}" diff --git a/add-req.sh b/add-req.sh index 72076133..75c84b85 100755 --- a/add-req.sh +++ b/add-req.sh @@ -21,33 +21,32 @@ for file in "${prompts[@]}"; do done dirs=( - # "auth" - # "automation" + "auth" + "automation" "basic" - # "bootstrap" + "bootstrap" "bot" - # "channels" + "channels" "config" "context" "drive_monitor" - # "email" - # "file" + "email" + "file" # "kb" - # "llm" - # "llm_models" - # "org" - # "package" - # "package_manager" - # "riot_compiler" + "llm" + "llm_models" + "org" + "package" + "package_manager" + "riot_compiler" "session" "shared" - # "tests" - # "tools" - # "ui" - # "web_server" - # "web_automation" - # "whatsapp" -) + "tests" + "tools" + "ui" + "web_server" + "web_automation" + ) filter_rust_file() { sed -E '/^\s*\/\//d' "$1" | \ diff --git a/src/automation/mod.rs b/src/automation/mod.rs index c68354f3..9f98226e 100644 --- a/src/automation/mod.rs +++ b/src/automation/mod.rs @@ -161,3 +161,27 @@ match script_service.compile(&script_content) { Ok(()) } } + +pub async fn execute_compact_prompt() -> Result<(), Box> { + use crate::shared::models::system_automations::dsl::{system_automations, is_active}; + use diesel::prelude::*; + use log::info; + use std::sync::Arc; + + let state = Arc::new(crate::shared::state::AppState::default()); + let service = AutomationService::new(Arc::clone(&state)); + + let mut conn = state.conn.lock().map_err(|e| format!("Failed to acquire lock: {}", e))?; + let automations: Vec = system_automations + .filter(is_active.eq(true)) + .load::(&mut *conn)?; + + for automation in automations { + if let Err(e) = service.execute_compact_prompt(&automation).await { + error!("Failed to compact prompt for bot {}: {}", automation.bot_id, e); + } + } + + info!("Prompt compaction cycle completed"); + Ok(()) +} diff --git a/src/basic/compiler/mod.rs b/src/basic/compiler/mod.rs index 58c8511d..9fdf5c72 100644 --- a/src/basic/compiler/mod.rs +++ b/src/basic/compiler/mod.rs @@ -365,7 +365,7 @@ impl BasicCompiler { for line in source.lines() { let trimmed = line.trim(); - if trimmed.is_empty() || trimmed.starts_with("//") || trimmed.starts_with("REM") { + if trimmed.is_empty() || trimmed.starts_with("'") || trimmed.starts_with("//") || trimmed.starts_with("REM") { continue; } diff --git a/src/bot/mod.rs b/src/bot/mod.rs index dc616e6d..e946115e 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -47,10 +47,15 @@ pub struct BotOrchestrator { impl BotOrchestrator { pub fn new(state: Arc) -> Self { - Self { + let orchestrator = Self { state, mounted_bots: Arc::new(AsyncMutex::new(HashMap::new())), - } + }; + + // Spawn internal automation to run compact prompt every minute if enabled + // Compact automation disabled to avoid Send issues in background task + + orchestrator } pub async fn mount_all_bots(&self) -> Result<(), Box> { @@ -412,7 +417,7 @@ impl BotOrchestrator { let mut prompt = String::new(); if !system_prompt.is_empty() { - prompt.push_str(&format!("SYSTEM: *** {} *** \n", system_prompt)); + prompt.push_str(&format!("SYS: *** {} *** \n", system_prompt)); } if !context_data.is_empty() { prompt.push_str(&format!("CONTEXT: *** {} *** \n", context_data)); @@ -549,13 +554,20 @@ impl BotOrchestrator { if last_progress_update.elapsed() >= progress_interval { let current_tokens = initial_tokens + crate::shared::utils::estimate_token_count(&full_response); if let Ok(metrics) = get_system_metrics(current_tokens, max_context_size) { - eprintln!( - "\nNVIDIA: {:.1}% | CPU: {:.1}% | Tokens: {}/{}", - metrics.gpu_usage.unwrap_or(0.0), - metrics.cpu_usage, - current_tokens, - max_context_size - ); +let gpu_bar = "█".repeat((metrics.gpu_usage.unwrap_or(0.0) / 5.0).round() as usize); +let cpu_bar = "█".repeat((metrics.cpu_usage / 5.0).round() as usize); +let token_ratio = current_tokens as f64 / max_context_size.max(1) as f64; +let token_bar = "█".repeat((token_ratio * 20.0).round() as usize); +eprintln!( + "\nGPU [{:<20}] {:.1}% | CPU [{:<20}] {:.1}% | TOKENS [{:<20}] {}/{}", + gpu_bar, + metrics.gpu_usage.unwrap_or(0.0), + cpu_bar, + metrics.cpu_usage, + token_bar, + current_tokens, + max_context_size +); } last_progress_update = Instant::now(); } @@ -582,10 +594,34 @@ impl BotOrchestrator { } } - trace!( - "Stream processing completed, {} chunks processed", - chunk_count - ); +trace!( + "Stream processing completed, {} chunks processed", + chunk_count +); + +// Sum tokens from all p.push context builds before submission +let total_tokens = crate::shared::utils::estimate_token_count(&prompt) + + crate::shared::utils::estimate_token_count(&context_data) + + crate::shared::utils::estimate_token_count(&full_response); +info!("Total tokens (context + prompt + response): {}", total_tokens); + +// Trigger compact prompt if enabled +let config_manager = ConfigManager::new(Arc::clone(&self.state.conn)); +let compact_enabled = config_manager + .get_config(&Uuid::parse_str(&message.bot_id).unwrap_or_default(), "prompt-compact", None) + .unwrap_or_default() + .parse::() + .unwrap_or(0); +if compact_enabled > 0 { + tokio::task::spawn_blocking(move || { + loop { + if let Err(e) = tokio::runtime::Handle::current().block_on(crate::automation::execute_compact_prompt()) { + error!("Failed to execute compact prompt: {}", e); + } + std::thread::sleep(Duration::from_secs(60)); + } + }); +} // Save final message with short lock scope { diff --git a/templates/announcements.gbai/annoucements.gbot/config.csv b/templates/announcements.gbai/annoucements.gbot/config.csv index 131e6cb1..372e1dba 100644 --- a/templates/announcements.gbai/annoucements.gbot/config.csv +++ b/templates/announcements.gbai/annoucements.gbot/config.csv @@ -1,6 +1,7 @@ name,value prompt-history, 2 -theme-color1,#0d2b55 -theme-color2,#fff9c2 -theme-logo,https://example.com/logo.png +prompt-compact, 4 +theme-color1, #0d2b55 +theme-color2, #fff9c2 +theme-logo, https://pragmatismo.com.br/icons/general-bots.svg theme-title, Custom