diff --git a/src/basic/keywords/add_suggestion.rs b/src/basic/keywords/add_suggestion.rs index 99eccd4f..bc2c177c 100644 --- a/src/basic/keywords/add_suggestion.rs +++ b/src/basic/keywords/add_suggestion.rs @@ -37,7 +37,22 @@ pub fn add_suggestion_keyword(state: Arc, user: UserSession, engine: & .await; match result { - Ok(_) => debug!("Suggestion added successfully to Redis key {}", redis_key), + Ok(_) => { + debug!("Suggestion added successfully to Redis key {}", redis_key); + + // Also register context as inactive initially + let active_key = format!("active_context:{}:{}", user.user_id, user.id); + let _: Result<(), redis::RedisError> = redis::cmd("HSET") + .arg(&active_key) + .arg(&context_name) + .arg("inactive") + .query_async(&mut conn) + .await + .unwrap_or_else(|e| { + error!("Failed to set context state: {}", e); + () + }); + } Err(e) => error!("Failed to add suggestion to Redis: {}", e), } }); diff --git a/web/html/index.html b/web/html/index.html index bfae8d91..69a13478 100644 --- a/web/html/index.html +++ b/web/html/index.html @@ -907,12 +907,24 @@ 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}`); + if (ws && ws.readyState === WebSocket.OPEN) { + const suggestionEvent = { + bot_id: "default_bot", + user_id: currentUserId, + session_id: currentSessionId, + channel: "web", + content: context, + message_type: 4, // custom type for suggestion click + is_suggestion: true, + context_name: context, + timestamp: new Date().toISOString() + }; + ws.send(JSON.stringify(suggestionEvent)); + alert(`Contexto alterado para: ${context}`); + } else { + console.warn("WebSocket não está conectado. Tentando reconectar..."); + connectWebSocket(); + } } catch (err) { console.error('Failed to set context:', err); }