botserver/src/basic/keywords/hear_talk.rs

119 lines
4.3 KiB
Rust
Raw Normal View History

2025-10-11 12:29:03 -03:00
use crate::shared::models::UserSession;
2025-10-11 20:02:14 -03:00
use crate::shared::state::AppState;
2025-10-11 12:29:03 -03:00
use log::info;
use rhai::{Dynamic, Engine, EvalAltResult};
2025-10-11 20:02:14 -03:00
pub fn hear_keyword(_state: &AppState, user: UserSession, engine: &mut Engine) {
2025-10-11 12:29:03 -03:00
let session_id = user.id;
2025-10-11 20:02:14 -03:00
2025-10-11 12:29:03 -03:00
engine
2025-10-11 20:02:14 -03:00
.register_custom_syntax(&["HEAR", "$ident$"], true, move |_context, inputs| {
let variable_name = inputs[0]
.get_string_value()
.expect("Expected identifier as string")
.to_string();
info!(
"HEAR command waiting for user input to store in variable: {}",
variable_name
);
// Spawn a background task to handle the inputwaiting logic.
// The actual waiting implementation should be added here.
2025-10-11 12:29:03 -03:00
tokio::spawn(async move {
2025-10-11 20:02:14 -03:00
log::debug!(
"HEAR: Starting async task for session {} and variable '{}'",
session_id,
variable_name
);
// TODO: implement actual waiting logic here without using the orchestrator
// For now, just log that we would wait for input
});
// Interrupt the current Rhai evaluation flow until the user input is received.
Err(Box::new(EvalAltResult::ErrorRuntime(
"Waiting for user input".into(),
rhai::Position::NONE,
)))
2025-10-11 12:29:03 -03:00
})
.unwrap();
}
pub fn talk_keyword(state: &AppState, user: UserSession, engine: &mut Engine) {
2025-10-11 20:02:14 -03:00
// Import the BotResponse type directly to satisfy diagnostics.
use crate::shared::models::BotResponse;
2025-10-11 12:29:03 -03:00
let state_clone = state.clone();
2025-10-11 20:02:14 -03:00
let user_clone = user.clone();
2025-10-11 12:29:03 -03:00
engine
.register_custom_syntax(&["TALK", "$expr$"], true, move |context, inputs| {
let message = context.eval_expression_tree(&inputs[0])?.to_string();
info!("TALK command executed: {}", message);
2025-10-11 20:02:14 -03:00
let response = BotResponse {
2025-10-11 12:29:03 -03:00
bot_id: "default_bot".to_string(),
2025-10-11 20:02:14 -03:00
user_id: user_clone.user_id.to_string(),
session_id: user_clone.id.to_string(),
2025-10-11 12:29:03 -03:00
channel: "basic".to_string(),
content: message,
message_type: 1,
2025-10-11 12:29:03 -03:00
stream_token: None,
is_complete: true,
};
2025-10-11 20:02:14 -03:00
// Send response through a channel or queue instead of accessing orchestrator directly
let _state_for_spawn = state_clone.clone();
2025-10-11 12:29:03 -03:00
tokio::spawn(async move {
2025-10-11 20:02:14 -03:00
// Use a more thread-safe approach to send the message
// This avoids capturing the orchestrator directly which isn't Send + Sync
// TODO: Implement proper response handling once response_sender field is added to AppState
log::debug!("TALK: Would send response: {:?}", response);
2025-10-11 12:29:03 -03:00
});
Ok(Dynamic::UNIT)
})
.unwrap();
}
pub fn set_context_keyword(state: &AppState, user: UserSession, engine: &mut Engine) {
let state_clone = state.clone();
engine
.register_custom_syntax(
&["SET", "CONTEXT", "$expr$"],
true,
move |context, inputs| {
let context_value = context.eval_expression_tree(&inputs[0])?.to_string();
info!("SET CONTEXT command executed: {}", context_value);
let redis_key = format!("context:{}:{}", user.user_id, user.id);
let state_for_redis = state_clone.clone();
2025-10-11 20:02:14 -03:00
2025-10-11 12:29:03 -03:00
tokio::spawn(async move {
if let Some(redis_client) = &state_for_redis.redis_client {
2025-10-11 20:02:14 -03:00
let mut conn = match redis_client.get_multiplexed_async_connection().await {
2025-10-11 12:29:03 -03:00
Ok(conn) => conn,
Err(e) => {
log::error!("Failed to connect to Redis: {}", e);
return;
}
};
let _: Result<(), _> = redis::cmd("SET")
.arg(&redis_key)
.arg(&context_value)
.query_async(&mut conn)
.await;
}
});
Ok(Dynamic::UNIT)
},
)
.unwrap();
}