2025-10-29 18:54:33 -03:00
|
|
|
use crate::shared::state::AppState;
|
|
|
|
|
use crate::shared::models::UserSession;
|
2025-11-02 14:16:35 -03:00
|
|
|
use log::{trace, debug, error, info};
|
2025-10-29 18:54:33 -03:00
|
|
|
use rhai::{Dynamic, Engine};
|
|
|
|
|
use serde_json::json;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2025-11-02 14:21:38 -03:00
|
|
|
pub fn clear_suggestions_keyword(state: Arc<AppState>, user_session: UserSession, engine: &mut Engine) {
|
|
|
|
|
let cache = state.cache.clone();
|
|
|
|
|
|
|
|
|
|
engine
|
2025-11-02 15:13:47 -03:00
|
|
|
.register_custom_syntax(&["CLEAR_SUGGESTIONS"], true, move |_context, _inputs| {
|
2025-11-02 14:21:38 -03:00
|
|
|
info!("CLEAR_SUGGESTIONS command executed");
|
|
|
|
|
|
|
|
|
|
if let Some(cache_client) = &cache {
|
|
|
|
|
let redis_key = format!("suggestions:{}:{}", user_session.user_id, user_session.id);
|
|
|
|
|
|
|
|
|
|
let mut conn = match cache_client.get_connection() {
|
|
|
|
|
Ok(conn) => conn,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!("Failed to connect to cache: {}", e);
|
|
|
|
|
return Ok(Dynamic::UNIT);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Delete the suggestions list
|
|
|
|
|
let result: Result<i64, redis::RedisError> = redis::cmd("DEL")
|
|
|
|
|
.arg(&redis_key)
|
|
|
|
|
.query(&mut conn);
|
|
|
|
|
|
|
|
|
|
match result {
|
|
|
|
|
Ok(deleted) => {
|
|
|
|
|
trace!("Cleared suggestions from Redis key {}, deleted: {}", redis_key, deleted);
|
|
|
|
|
}
|
|
|
|
|
Err(e) => error!("Failed to clear suggestions from Redis: {}", e),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
debug!("No Cache client configured; suggestions not cleared");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Dynamic::UNIT)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 20:53:45 -03:00
|
|
|
pub fn add_suggestion_keyword(state: Arc<AppState>, user_session: UserSession, engine: &mut Engine) {
|
|
|
|
|
let cache = state.cache.clone();
|
2025-10-29 18:54:33 -03:00
|
|
|
|
|
|
|
|
engine
|
2025-11-01 20:53:45 -03:00
|
|
|
.register_custom_syntax(&["ADD_SUGGESTION", "$expr$", "AS", "$expr$"], true, move |context, inputs| {
|
2025-10-29 18:54:33 -03:00
|
|
|
let context_name = context.eval_expression_tree(&inputs[0])?.to_string();
|
|
|
|
|
let button_text = context.eval_expression_tree(&inputs[1])?.to_string();
|
|
|
|
|
|
|
|
|
|
info!("ADD_SUGGESTION command executed: context='{}', text='{}'", context_name, button_text);
|
|
|
|
|
|
|
|
|
|
if let Some(cache_client) = &cache {
|
2025-11-01 20:53:45 -03:00
|
|
|
let redis_key = format!("suggestions:{}:{}", user_session.user_id, user_session.id);
|
2025-10-29 18:54:33 -03:00
|
|
|
let suggestion = json!({ "context": context_name, "text": button_text });
|
|
|
|
|
|
2025-11-02 10:45:57 -03:00
|
|
|
let mut conn = match cache_client.get_connection() {
|
|
|
|
|
Ok(conn) => conn,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!("Failed to connect to cache: {}", e);
|
|
|
|
|
return Ok(Dynamic::UNIT);
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-10-29 18:54:33 -03:00
|
|
|
|
2025-11-02 10:45:57 -03:00
|
|
|
// Append suggestion to Redis list - RPUSH returns the new length as i64
|
|
|
|
|
let result: Result<i64, redis::RedisError> = redis::cmd("RPUSH")
|
|
|
|
|
.arg(&redis_key)
|
|
|
|
|
.arg(suggestion.to_string())
|
|
|
|
|
.query(&mut conn);
|
2025-10-29 18:54:33 -03:00
|
|
|
|
2025-11-02 10:45:57 -03:00
|
|
|
match result {
|
|
|
|
|
Ok(length) => {
|
2025-11-02 14:16:35 -03:00
|
|
|
trace!("Suggestion added successfully to Redis key {}, new length: {}", redis_key, length);
|
2025-10-30 14:53:06 -03:00
|
|
|
|
2025-11-02 10:45:57 -03:00
|
|
|
// Also register context as inactive initially
|
|
|
|
|
let active_key = format!("active_context:{}:{}", user_session.user_id, user_session.id);
|
|
|
|
|
let hset_result: Result<i64, redis::RedisError> = redis::cmd("HSET")
|
|
|
|
|
.arg(&active_key)
|
|
|
|
|
.arg(&context_name)
|
|
|
|
|
.arg("inactive")
|
|
|
|
|
.query(&mut conn);
|
2025-10-31 15:58:54 -03:00
|
|
|
|
2025-11-02 10:45:57 -03:00
|
|
|
match hset_result {
|
|
|
|
|
Ok(fields_added) => {
|
2025-11-02 14:16:35 -03:00
|
|
|
trace!("Context state set to inactive for {}, fields added: {}", context_name, fields_added)
|
2025-11-02 10:45:57 -03:00
|
|
|
},
|
|
|
|
|
Err(e) => error!("Failed to set context state: {}", e),
|
2025-10-30 14:53:06 -03:00
|
|
|
}
|
2025-10-29 18:54:33 -03:00
|
|
|
}
|
2025-11-02 10:45:57 -03:00
|
|
|
Err(e) => error!("Failed to add suggestion to Redis: {}", e),
|
|
|
|
|
}
|
2025-10-29 18:54:33 -03:00
|
|
|
} else {
|
2025-11-02 14:16:35 -03:00
|
|
|
debug!("No Cache client configured; suggestion will not persist");
|
2025-10-29 18:54:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Dynamic::UNIT)
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2025-11-02 10:45:57 -03:00
|
|
|
}
|