fix: remove blocking recv_timeout from LLM keyword
All checks were successful
BotServer CI/CD / build (push) Successful in 3m41s

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2026-04-13 23:01:54 -03:00
parent 6468588f58
commit b3fd90b056

View file

@ -18,40 +18,25 @@ pub fn llm_keyword(state: Arc<AppState>, _user: UserSession, engine: &mut Engine
let text = context let text = context
.eval_expression_tree(first_input)? .eval_expression_tree(first_input)?
.to_string(); .to_string();
let state_for_thread = Arc::clone(&state_clone); let state_for_async = Arc::clone(&state_clone);
let prompt = build_llm_prompt(&text); let prompt = build_llm_prompt(&text);
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || { let handle = tokio::runtime::Handle::current();
let rt = tokio::runtime::Builder::new_multi_thread() let result = handle.block_on(async move {
.worker_threads(2) tokio::time::timeout(
.enable_all() Duration::from_secs(45),
.build(); execute_llm_generation(state_for_async, prompt)
let send_err = if let Ok(rt) = rt { ).await
let result = rt.block_on(async move {
execute_llm_generation(state_for_thread, prompt).await
}); });
tx.send(result).err()
} else { match result {
tx.send(Err("failed to build tokio runtime".into())).err() Ok(Ok(output)) => Ok(Dynamic::from(output)),
};
if send_err.is_some() {
error!("Failed to send LLM thread result");
}
});
match rx.recv_timeout(Duration::from_secs(500)) {
Ok(Ok(result)) => Ok(Dynamic::from(result)),
Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime( Ok(Err(e)) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
e.to_string().into(), e.to_string().into(),
rhai::Position::NONE, rhai::Position::NONE,
))), ))),
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => { Err(_) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
Err(Box::new(rhai::EvalAltResult::ErrorRuntime( "LLM generation timed out after 45 seconds".into(),
"LLM generation timed out".into(),
rhai::Position::NONE,
)))
}
Err(e) => Err(Box::new(rhai::EvalAltResult::ErrorRuntime(
format!("LLM thread failed: {e}").into(),
rhai::Position::NONE, rhai::Position::NONE,
))), ))),
} }