feat(suggestions): enhance suggestion handling and context management in WebSocket
This commit is contained in:
parent
2b906b7a9d
commit
4337bd4229
2 changed files with 34 additions and 7 deletions
|
|
@ -37,7 +37,22 @@ pub fn add_suggestion_keyword(state: Arc<AppState>, user: UserSession, engine: &
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
match result {
|
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),
|
Err(e) => error!("Failed to add suggestion to Redis: {}", e),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -907,12 +907,24 @@
|
||||||
|
|
||||||
async function setContext(context) {
|
async function setContext(context) {
|
||||||
try {
|
try {
|
||||||
await fetch('/api/set_context', {
|
if (ws && ws.readyState === WebSocket.OPEN) {
|
||||||
method: 'POST',
|
const suggestionEvent = {
|
||||||
headers: { 'Content-Type': 'application/json' },
|
bot_id: "default_bot",
|
||||||
body: JSON.stringify({ context })
|
user_id: currentUserId,
|
||||||
});
|
session_id: currentSessionId,
|
||||||
alert(`Contexto alterado para: ${context}`);
|
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) {
|
} catch (err) {
|
||||||
console.error('Failed to set context:', err);
|
console.error('Failed to set context:', err);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue