Merge pull request #445 from ChristopherCastilho/main
Add_suggestion fix
This commit is contained in:
commit
683955a4a4
6 changed files with 130 additions and 1 deletions
3
.cline/config.json
Normal file
3
.cline/config.json
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"checkpoints": false
|
||||
}
|
||||
69
src/basic/keywords/add_suggestion.rs
Normal file
69
src/basic/keywords/add_suggestion.rs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
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 - RPUSH returns the new length as i64
|
||||
let result: Result<i64, redis::RedisError> = redis::cmd("RPUSH")
|
||||
.arg(&redis_key)
|
||||
.arg(suggestion.to_string())
|
||||
.query_async(&mut conn)
|
||||
.await;
|
||||
|
||||
match result {
|
||||
Ok(length) => {
|
||||
debug!("Suggestion added successfully to Redis key {}, new length: {}", redis_key, length);
|
||||
|
||||
// Also register context as inactive initially
|
||||
let active_key = format!("active_context:{}:{}", user.user_id, user.id);
|
||||
let hset_result: Result<i64, redis::RedisError> = redis::cmd("HSET")
|
||||
.arg(&active_key)
|
||||
.arg(&context_name)
|
||||
.arg("inactive")
|
||||
.query_async(&mut conn)
|
||||
.await;
|
||||
|
||||
match hset_result {
|
||||
Ok(fields_added) => {
|
||||
debug!("Context state set to inactive for {}, fields added: {}", context_name, fields_added)
|
||||
},
|
||||
Err(e) => error!("Failed to set context state: {}", e),
|
||||
}
|
||||
}
|
||||
Err(e) => error!("Failed to add suggestion to Redis: {}", e),
|
||||
}
|
||||
});
|
||||
} else {
|
||||
debug!("No Redis client configured; suggestion will not persist");
|
||||
}
|
||||
|
||||
Ok(Dynamic::UNIT)
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ use self::keywords::set::set_keyword;
|
|||
use self::keywords::set_kb::{add_kb_keyword, set_kb_keyword};
|
||||
use self::keywords::set_schedule::set_schedule_keyword;
|
||||
use self::keywords::wait::wait_keyword;
|
||||
use self::keywords::add_suggestion::add_suggestion_keyword;
|
||||
|
||||
#[cfg(feature = "email")]
|
||||
use self::keywords::create_draft_keyword;
|
||||
|
|
@ -81,6 +82,7 @@ impl ScriptService {
|
|||
clear_tools_keyword(state.clone(), user.clone(), &mut engine);
|
||||
list_tools_keyword(state.clone(), user.clone(), &mut engine);
|
||||
add_website_keyword(state.clone(), user.clone(), &mut engine);
|
||||
add_suggestion_keyword(state.clone(), user.clone(), &mut engine);
|
||||
|
||||
#[cfg(feature = "web_automation")]
|
||||
get_website_keyword(&state, user.clone(), &mut engine);
|
||||
|
|
|
|||
|
|
@ -841,4 +841,4 @@ post_install_cmds_linux: vec![
|
|||
hasher.update(password.as_bytes());
|
||||
format!("{:x}", hasher.finalize())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -878,6 +878,60 @@
|
|||
/>
|
||||
<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 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('load', loadSuggestions);
|
||||
</script>
|
||||
</footer>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue