feat(keywords, ui): add suggestion module and UI for context switching

Added new `add_suggestion` module to support suggestion handling logic.
Updated `index.html` to include a dynamic suggestions container that fetches and displays context suggestions.
This improves user experience by enabling quick context changes through interactive suggestion buttons.
This commit is contained in:
christopher 2025-10-29 18:54:33 -03:00
parent 52e2f79395
commit d278b95fac
4 changed files with 97 additions and 0 deletions

3
.cline/config.json Normal file
View file

@ -0,0 +1,3 @@
{
"checkpoints": false
}

View file

@ -0,0 +1,51 @@
use crate::shared::state::AppState;
use crate::shared::models::UserSession;
use log::{debug, error, info};
use rhai::{Dynamic, Engine};
use serde_json::json;
use std::sync::Arc;
pub fn add_suggestion_keyword(state: Arc<AppState>, user: UserSession, engine: &mut Engine) {
let cache = state.redis_client.clone();
engine
.register_custom_syntax(&["ADD_SUGGESTION", "$expr$", "$expr$"], true, move |context, inputs| {
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 {
let cache_client = cache_client.clone();
let redis_key = format!("suggestions:{}:{}", user.user_id, user.id);
let suggestion = json!({ "context": context_name, "text": button_text });
tokio::spawn(async move {
let mut conn = match cache_client.get_multiplexed_async_connection().await {
Ok(conn) => conn,
Err(e) => {
error!("Failed to connect to cache: {}", e);
return;
}
};
// Append suggestion to Redis list
let result: Result<(), redis::RedisError> = redis::cmd("RPUSH")
.arg(&redis_key)
.arg(suggestion.to_string())
.query_async(&mut conn)
.await;
match result {
Ok(_) => debug!("Suggestion added successfully to Redis key {}", redis_key),
Err(e) => error!("Failed to add suggestion to Redis: {}", e),
}
});
} else {
debug!("No Redis client configured; suggestion will not persist");
}
Ok(Dynamic::UNIT)
})
.unwrap();
}

View file

@ -19,6 +19,7 @@ pub mod set;
pub mod set_kb;
pub mod set_schedule;
pub mod wait;
pub mod add_suggestion;
#[cfg(feature = "email")]
pub mod create_draft_keyword;

View file

@ -878,6 +878,48 @@
/>
<button id="sendBtn">Enviar</button>
</div>
<div id="suggestions-container" style="text-align:center; margin-top:10px;"></div>
<script>
async function loadSuggestions() {
try {
const res = await fetch('/api/suggestions');
const suggestions = await res.json();
const container = document.getElementById('suggestions-container');
container.innerHTML = '';
suggestions.forEach(s => {
const btn = document.createElement('button');
btn.textContent = s.text;
btn.className = 'suggestion-button';
btn.style.margin = '5px';
btn.style.padding = '8px 12px';
btn.style.backgroundColor = '#ffd700';
btn.style.color = '#0a0e27';
btn.style.border = 'none';
btn.style.borderRadius = '6px';
btn.style.cursor = 'pointer';
btn.onclick = () => setContext(s.context);
container.appendChild(btn);
});
} catch (err) {
console.error('Failed to load suggestions:', err);
}
}
async function setContext(context) {
try {
await fetch('/api/set_context', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ context })
});
alert(`Contexto alterado para: ${context}`);
} catch (err) {
console.error('Failed to set context:', err);
}
}
window.addEventListener('load', loadSuggestions);
</script>
</footer>
</div>