From cded41d305b53076d4a63246d6503fb145326b8a Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (Pragmatismo)" Date: Sun, 2 Nov 2025 14:21:38 -0300 Subject: [PATCH] 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. --- src/basic/keywords/add_suggestion.rs | 38 +++++++++++++++++++ .../announcements.gbdialog/start.bas | 1 + 2 files changed, 39 insertions(+) diff --git a/src/basic/keywords/add_suggestion.rs b/src/basic/keywords/add_suggestion.rs index b4324aac4..355748f4d 100644 --- a/src/basic/keywords/add_suggestion.rs +++ b/src/basic/keywords/add_suggestion.rs @@ -5,6 +5,44 @@ use rhai::{Dynamic, Engine}; use serde_json::json; use std::sync::Arc; +pub fn clear_suggestions_keyword(state: Arc, 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 = 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, user_session: UserSession, engine: &mut Engine) { let cache = state.cache.clone(); diff --git a/templates/announcements.gbai/announcements.gbdialog/start.bas b/templates/announcements.gbai/announcements.gbdialog/start.bas index 0efb307dc..c4dbd26e5 100644 --- a/templates/announcements.gbai/announcements.gbdialog/start.bas +++ b/templates/announcements.gbai/announcements.gbdialog/start.bas @@ -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?"