feat(suggestions): enhance suggestion handling and context management in WebSocket

This commit is contained in:
christopher 2025-10-30 14:53:06 -03:00
parent 2b906b7a9d
commit 4337bd4229
2 changed files with 34 additions and 7 deletions

View file

@ -37,7 +37,22 @@ pub fn add_suggestion_keyword(state: Arc<AppState>, 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),
}
});

View file

@ -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);
}