From 3f1a4d113e71fc412ec05a319d5ffd5a3d7a78bc Mon Sep 17 00:00:00 2001 From: salmonik Date: Fri, 31 Jul 2026 01:09:07 +0200 Subject: fir auto completion and send file in the response body upon grajcuj file --- src/events/mod.rs | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'src/events/mod.rs') diff --git a/src/events/mod.rs b/src/events/mod.rs index d166993..82b6f19 100644 --- a/src/events/mod.rs +++ b/src/events/mod.rs @@ -130,8 +130,7 @@ pub async fn handle_voice_state_update( let mut tts_channel: Option = None; // ── JOIN ── - if old_channel.is_none() && new_channel.is_some() { - let cid = new_channel.unwrap(); + if old_channel.is_none() && let Some(cid) = new_channel { let is_afk = new.self_mute || new.self_deaf; va.join_time = now; @@ -332,11 +331,18 @@ pub async fn handle_message( let has_keyword = content_lower.contains(&data.config.assistant_keyword); if is_mention || has_keyword { + // Spawn the LLM call in a background task so it doesn't block the + // event loop. Discord interactions must be answered within 3 s; a + // 5–10 s OpenRouter call would make every subsequent interaction + // expire with "Unknown interaction". + let http = ctx.http.clone(); + let channel_id = new_message.channel_id; + let _reply_to_id = new_message.id; + let bot_id = ctx.cache.current_user().id; let (api_url, api_key, model) = data.config.resolve_llm(); - let client = crate::openrouter::OpenRouterClient::new(api_url, api_key, model); - let assistant_prompt = lang::ASSISTANT_SYSTEM_PROMPT; + let assistant_prompt = lang::ASSISTANT_SYSTEM_PROMPT.to_string(); - let bot_id = ctx.cache.current_user().id; + // Build message history synchronously (fast path). let mut messages = Vec::new(); let mut current_msg = Some(new_message.clone()); for _ in 0..5 { @@ -346,9 +352,9 @@ pub async fn handle_message( } else { "user" }; - messages.push((role.into(), msg.content.clone())); + messages.push((role.to_string(), msg.content.clone())); current_msg = match msg.message_reference.and_then(|r| r.message_id) { - Some(ref_id) => msg.channel_id.message(ctx, ref_id).await.ok(), + Some(ref_id) => channel_id.message(&http, ref_id).await.ok(), None => None, }; } else { @@ -357,9 +363,12 @@ pub async fn handle_message( } messages.reverse(); - if let Ok(response) = client.chat_with_history(assistant_prompt, messages).await { - new_message.reply(ctx, response).await.ok(); - } + tokio::spawn(async move { + let client = crate::openrouter::OpenRouterClient::new(api_url, api_key, model); + if let Ok(response) = client.chat_with_history(&assistant_prompt, messages).await { + let _ = channel_id.say(&http, response).await; + } + }); } Ok(()) -- cgit v1.2.3