generalbots/src/basic/keywords/set_context.rs
Rodrigo Rodriguez (Pragmatismo) 73d0ab3a1a refactor: simplify UI panels, use pooled DB, add --noui flag
- Removed unused `id` and `app_state` fields from `ChatPanel`; updated constructor to accept but ignore the state, reducing memory footprint.
- Switched database access in `ChatPanel` from a raw `Mutex` lock to a connection pool (`app_state.conn.get()`), improving concurrency and error handling.
- Reordered and cleaned up imports in `status_panel.rs` and formatted struct fields for readability.
- Updated VS Code launch configuration to pass `--noui` argument, enabling headless mode for debugging.
- Bumped several crate versions in `Cargo.lock` (e.g., `bitflags` to 2.10.0, `syn` to 2.0.108, `cookie` to 0.16.2) and added the new `ashpd` dependency, aligning the project with latest library releases.
2025-11-11 09:42:52 -03:00

51 lines
1.8 KiB
Rust

use std::sync::Arc;
use log::trace;
use log::{error};
use crate::shared::state::AppState;
use crate::shared::models::UserSession;
use rhai::Engine;
use rhai::Dynamic;
pub fn set_context_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
let cache = state.cache.clone();
engine
.register_custom_syntax(&["SET_CONTEXT", "$expr$", "AS", "$expr$"], true, move |context, inputs| {
let context_name = context.eval_expression_tree(&inputs[0])?.to_string();
let context_value = context.eval_expression_tree(&inputs[1])?.to_string();
trace!("SET CONTEXT command executed - name: {}, value: {}", context_name, context_value);
let redis_key = format!("context:{}:{}:{}", user.user_id, user.id, context_name);
trace!("Constructed Redis key: {} for user {}, session {}, context {}", redis_key, user.user_id, user.id, context_name);
if let Some(cache_client) = &cache {
let cache_client = cache_client.clone();
let redis_key = redis_key.clone();
let context_value = context_value.clone();
trace!("Cloned cache_client, redis_key ({}) and context_value (len={}) for async task", redis_key, context_value.len());
tokio::spawn(async move {
let mut conn = match cache_client.get_multiplexed_async_connection().await {
Ok(conn) => {
trace!("Cache connection established successfully");
conn
}
Err(e) => {
error!("Failed to connect to cache: {}", e);
return;
}
};
trace!("Executing Redis SET command with key: {} and value length: {}", redis_key, context_value.len());
let result: Result<(), redis::RedisError> = redis::cmd("SET").arg(&redis_key).arg(&context_value).query_async(&mut conn).await;
match result {
Ok(_) => {
trace!("Context value successfully stored in cache");
}
Err(e) => {
error!("Failed to set cache value: {}", e);
}
}
});
} else {
trace!("No cache configured, context not persisted");
}
Ok(Dynamic::UNIT)
},
)
.unwrap();
}