feat(keywords): add CLEAR_SUGGESTIONS command and implement in dialog

Added new CLEAR_SUGGESTIONS keyword command that clears user suggestions from Redis cache. Implemented the command in the announcements dialog start script to prevent duplicate suggestions. The command handles Redis connection errors gracefully and logs appropriate debug information.
This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-11-02 14:21:38 -03:00
parent f2beef5d86
commit 87fc13b08a
2 changed files with 39 additions and 0 deletions

View file

@ -5,6 +5,44 @@ use rhai::{Dynamic, Engine};
use serde_json::json;
use std::sync::Arc;
pub fn clear_suggestions_keyword(state: Arc<AppState>, user_session: UserSession, engine: &mut Engine) {
let cache = state.cache.clone();
engine
.register_custom_syntax(&["CLEAR_SUGGESTIONS"], true, move |context, _inputs| {
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();
}
pub fn add_suggestion_keyword(state: Arc<AppState>, user_session: UserSession, engine: &mut Engine) {
let cache = state.cache.clone();

View file

@ -6,6 +6,7 @@ SET_CONTEXT "general" AS resume1;
SET_CONTEXT "auxiliom" AS resume2;
SET_CONTEXT "toolbix" AS resume3;
CLEAR_SUGGESTIONS;
ADD_SUGGESTION "general" AS "Show me the weekly announcements"
ADD_SUGGESTION "auxiliom" AS "Will Auxiliom help me with what?"