2025-10-18 18:20:02 -03:00
|
|
|
use crate::basic::compiler::BasicCompiler;
|
2025-11-01 09:18:02 -03:00
|
|
|
use crate::config::ConfigManager;
|
2025-10-18 18:20:02 -03:00
|
|
|
use crate::shared::state::AppState;
|
2025-10-30 12:35:25 -03:00
|
|
|
use aws_sdk_s3::Client;
|
2025-11-06 16:15:54 -03:00
|
|
|
use log::{info};
|
2025-10-18 18:20:02 -03:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::error::Error;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use tokio::time::{interval, Duration};
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct FileState {
|
|
|
|
|
pub etag: String,
|
|
|
|
|
}
|
|
|
|
|
pub struct DriveMonitor {
|
|
|
|
|
state: Arc<AppState>,
|
|
|
|
|
bucket_name: String,
|
|
|
|
|
file_states: Arc<tokio::sync::RwLock<HashMap<String, FileState>>>,
|
2025-11-01 09:38:15 -03:00
|
|
|
bot_id: uuid::Uuid,
|
2025-10-18 18:20:02 -03:00
|
|
|
}
|
|
|
|
|
impl DriveMonitor {
|
2025-11-01 09:38:15 -03:00
|
|
|
pub fn new(state: Arc<AppState>, bucket_name: String, bot_id: uuid::Uuid) -> Self {
|
2025-10-18 18:20:02 -03:00
|
|
|
Self {
|
|
|
|
|
state,
|
|
|
|
|
bucket_name,
|
|
|
|
|
file_states: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
|
2025-11-01 09:38:15 -03:00
|
|
|
bot_id,
|
2025-10-18 18:20:02 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn spawn(self: Arc<Self>) -> tokio::task::JoinHandle<()> {
|
|
|
|
|
tokio::spawn(async move {
|
2025-11-05 21:10:03 -03:00
|
|
|
info!("Drive Monitor service started for bucket: {}", self.bucket_name);
|
2025-10-28 14:00:52 -03:00
|
|
|
let mut tick = interval(Duration::from_secs(30));
|
2025-10-18 18:20:02 -03:00
|
|
|
loop {
|
|
|
|
|
tick.tick().await;
|
|
|
|
|
if let Err(e) = self.check_for_changes().await {
|
2025-11-05 21:10:03 -03:00
|
|
|
log::error!("Error checking for drive changes: {}", e);
|
2025-10-18 18:20:02 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
async fn check_for_changes(&self) -> Result<(), Box<dyn Error + Send + Sync>> {
|
2025-11-01 20:53:45 -03:00
|
|
|
let client = match &self.state.drive {
|
2025-10-30 12:35:25 -03:00
|
|
|
Some(client) => client,
|
2025-11-05 21:10:03 -03:00
|
|
|
None => return Ok(()),
|
2025-10-18 18:20:02 -03:00
|
|
|
};
|
2025-10-30 12:35:25 -03:00
|
|
|
self.check_gbdialog_changes(client).await?;
|
2025-11-05 21:10:03 -03:00
|
|
|
self.check_gbot(client).await?;
|
2025-10-18 18:20:02 -03:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-11-05 21:10:03 -03:00
|
|
|
async fn check_gbdialog_changes(&self, client: &Client) -> Result<(), Box<dyn Error + Send + Sync>> {
|
2025-10-18 18:20:02 -03:00
|
|
|
let prefix = ".gbdialog/";
|
|
|
|
|
let mut current_files = HashMap::new();
|
2025-10-30 12:35:25 -03:00
|
|
|
let mut continuation_token = None;
|
|
|
|
|
loop {
|
2025-11-11 10:34:06 -03:00
|
|
|
let list_objects = match tokio::time::timeout(
|
|
|
|
|
Duration::from_secs(30),
|
|
|
|
|
client
|
|
|
|
|
.list_objects_v2()
|
|
|
|
|
.bucket(&self.bucket_name.to_lowercase())
|
|
|
|
|
.set_continuation_token(continuation_token)
|
|
|
|
|
.send()
|
|
|
|
|
).await {
|
|
|
|
|
Ok(Ok(list)) => list,
|
|
|
|
|
Ok(Err(e)) => return Err(e.into()),
|
|
|
|
|
Err(_) => {
|
|
|
|
|
log::error!("Timeout listing objects in bucket {}", self.bucket_name);
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-10-30 12:35:25 -03:00
|
|
|
for obj in list_objects.contents.unwrap_or_default() {
|
|
|
|
|
let path = obj.key().unwrap_or_default().to_string();
|
2025-10-31 07:30:37 -03:00
|
|
|
let path_parts: Vec<&str> = path.split('/').collect();
|
|
|
|
|
if path_parts.len() < 2 || !path_parts[0].ends_with(".gbdialog") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-10-30 12:35:25 -03:00
|
|
|
if path.ends_with('/') || !path.ends_with(".bas") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let file_state = FileState {
|
|
|
|
|
etag: obj.e_tag().unwrap_or_default().to_string(),
|
|
|
|
|
};
|
|
|
|
|
current_files.insert(path, file_state);
|
2025-10-18 18:20:02 -03:00
|
|
|
}
|
2025-10-30 12:35:25 -03:00
|
|
|
if !list_objects.is_truncated.unwrap_or(false) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
continuation_token = list_objects.next_continuation_token;
|
2025-10-18 18:20:02 -03:00
|
|
|
}
|
|
|
|
|
let mut file_states = self.file_states.write().await;
|
|
|
|
|
for (path, current_state) in current_files.iter() {
|
|
|
|
|
if let Some(previous_state) = file_states.get(path) {
|
|
|
|
|
if current_state.etag != previous_state.etag {
|
2025-10-30 12:35:25 -03:00
|
|
|
if let Err(e) = self.compile_tool(client, path).await {
|
2025-11-05 21:10:03 -03:00
|
|
|
log::error!("Failed to compile tool {}: {}", path, e);
|
2025-10-18 18:20:02 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-10-30 12:35:25 -03:00
|
|
|
if let Err(e) = self.compile_tool(client, path).await {
|
2025-11-05 21:10:03 -03:00
|
|
|
log::error!("Failed to compile tool {}: {}", path, e);
|
2025-10-18 18:20:02 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let previous_paths: Vec<String> = file_states
|
|
|
|
|
.keys()
|
|
|
|
|
.filter(|k| k.starts_with(prefix))
|
|
|
|
|
.cloned()
|
|
|
|
|
.collect();
|
|
|
|
|
for path in previous_paths {
|
|
|
|
|
if !current_files.contains_key(&path) {
|
|
|
|
|
file_states.remove(&path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (path, state) in current_files {
|
|
|
|
|
file_states.insert(path, state);
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-11-01 09:38:15 -03:00
|
|
|
async fn check_gbot(&self, client: &Client) -> Result<(), Box<dyn Error + Send + Sync>> {
|
2025-11-11 09:42:52 -03:00
|
|
|
let config_manager = ConfigManager::new(self.state.conn.clone());
|
2025-10-31 07:30:37 -03:00
|
|
|
let mut continuation_token = None;
|
|
|
|
|
loop {
|
2025-11-11 10:34:06 -03:00
|
|
|
let list_objects = match tokio::time::timeout(
|
|
|
|
|
Duration::from_secs(30),
|
|
|
|
|
client
|
|
|
|
|
.list_objects_v2()
|
|
|
|
|
.bucket(&self.bucket_name.to_lowercase())
|
|
|
|
|
.set_continuation_token(continuation_token)
|
|
|
|
|
.send()
|
|
|
|
|
).await {
|
|
|
|
|
Ok(Ok(list)) => list,
|
|
|
|
|
Ok(Err(e)) => return Err(e.into()),
|
|
|
|
|
Err(_) => {
|
|
|
|
|
log::error!("Timeout listing objects in bucket {}", self.bucket_name);
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-10-31 07:30:37 -03:00
|
|
|
for obj in list_objects.contents.unwrap_or_default() {
|
|
|
|
|
let path = obj.key().unwrap_or_default().to_string();
|
|
|
|
|
let path_parts: Vec<&str> = path.split('/').collect();
|
|
|
|
|
if path_parts.len() < 2 || !path_parts[0].ends_with(".gbot") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if !path.ends_with("config.csv") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-11-05 21:10:03 -03:00
|
|
|
match client.head_object().bucket(&self.bucket_name).key(&path).send().await {
|
|
|
|
|
Ok(_head_res) => {
|
|
|
|
|
let response = client.get_object().bucket(&self.bucket_name).key(&path).send().await?;
|
2025-10-31 07:30:37 -03:00
|
|
|
let bytes = response.body.collect().await?.into_bytes();
|
|
|
|
|
let csv_content = String::from_utf8(bytes.to_vec())
|
|
|
|
|
.map_err(|e| format!("UTF-8 error in {}: {}", path, e))?;
|
2025-11-01 15:16:43 -03:00
|
|
|
let llm_lines: Vec<_> = csv_content
|
|
|
|
|
.lines()
|
|
|
|
|
.filter(|line| line.trim_start().starts_with("llm-"))
|
|
|
|
|
.collect();
|
|
|
|
|
if !llm_lines.is_empty() {
|
2025-11-02 18:36:21 -03:00
|
|
|
use crate::llm::local::ensure_llama_servers_running;
|
2025-11-01 15:16:43 -03:00
|
|
|
let mut restart_needed = false;
|
|
|
|
|
for line in llm_lines {
|
|
|
|
|
let parts: Vec<&str> = line.split(',').collect();
|
|
|
|
|
if parts.len() >= 2 {
|
|
|
|
|
let key = parts[0].trim();
|
|
|
|
|
let new_value = parts[1].trim();
|
|
|
|
|
match config_manager.get_config(&self.bot_id, key, None) {
|
|
|
|
|
Ok(old_value) => {
|
|
|
|
|
if old_value != new_value {
|
2025-11-05 21:10:03 -03:00
|
|
|
info!("Detected change in {} (old: {}, new: {})", key, old_value, new_value);
|
2025-11-01 14:23:40 -03:00
|
|
|
restart_needed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-01 15:16:43 -03:00
|
|
|
Err(_) => {
|
|
|
|
|
restart_needed = true;
|
|
|
|
|
}
|
2025-11-01 14:23:40 -03:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-01 15:16:43 -03:00
|
|
|
}
|
2025-11-05 21:10:03 -03:00
|
|
|
let _ = config_manager.sync_gbot_config(&self.bot_id, &csv_content);
|
2025-11-01 15:16:43 -03:00
|
|
|
if restart_needed {
|
2025-11-11 09:42:52 -03:00
|
|
|
if let Err(e) = ensure_llama_servers_running(Arc::clone(&self.state)).await {
|
2025-11-05 21:10:03 -03:00
|
|
|
log::error!("Failed to restart LLaMA servers after llm- config change: {}", e);
|
2025-11-01 14:23:40 -03:00
|
|
|
}
|
2025-11-01 15:16:43 -03:00
|
|
|
}
|
2025-11-05 21:10:03 -03:00
|
|
|
} else {
|
|
|
|
|
let _ = config_manager.sync_gbot_config(&self.bot_id, &csv_content);
|
2025-11-01 15:16:43 -03:00
|
|
|
}
|
2025-11-05 21:10:03 -03:00
|
|
|
if csv_content.lines().any(|line| line.starts_with("theme-")) {
|
|
|
|
|
self.broadcast_theme_change(&csv_content).await?;
|
2025-11-01 09:18:02 -03:00
|
|
|
}
|
2025-10-31 07:30:37 -03:00
|
|
|
}
|
|
|
|
|
Err(e) => {
|
2025-11-05 21:10:03 -03:00
|
|
|
log::error!("Config file {} not found or inaccessible: {}", path, e);
|
2025-10-31 07:30:37 -03:00
|
|
|
}
|
|
|
|
|
}
|
2025-10-26 14:15:43 -03:00
|
|
|
}
|
2025-10-31 07:30:37 -03:00
|
|
|
if !list_objects.is_truncated.unwrap_or(false) {
|
|
|
|
|
break;
|
2025-10-26 14:15:43 -03:00
|
|
|
}
|
2025-10-31 07:30:37 -03:00
|
|
|
continuation_token = list_objects.next_continuation_token;
|
2025-10-26 14:15:43 -03:00
|
|
|
}
|
2025-10-31 07:30:37 -03:00
|
|
|
Ok(())
|
2025-10-26 14:15:43 -03:00
|
|
|
}
|
2025-11-05 21:10:03 -03:00
|
|
|
async fn broadcast_theme_change(&self, csv_content: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
|
|
|
|
let mut theme_data = serde_json::json!({
|
|
|
|
|
"event": "change_theme",
|
|
|
|
|
"data": {}
|
|
|
|
|
});
|
|
|
|
|
for line in csv_content.lines() {
|
|
|
|
|
let parts: Vec<&str> = line.split(',').collect();
|
|
|
|
|
if parts.len() >= 2 {
|
|
|
|
|
let key = parts[0].trim();
|
|
|
|
|
let value = parts[1].trim();
|
|
|
|
|
match key {
|
|
|
|
|
"theme-color1" => theme_data["data"]["color1"] = serde_json::Value::String(value.to_string()),
|
|
|
|
|
"theme-color2" => theme_data["data"]["color2"] = serde_json::Value::String(value.to_string()),
|
|
|
|
|
"theme-logo" => theme_data["data"]["logo_url"] = serde_json::Value::String(value.to_string()),
|
|
|
|
|
"theme-title" => theme_data["data"]["title"] = serde_json::Value::String(value.to_string()),
|
|
|
|
|
"theme-logo-text" => theme_data["data"]["logo_text"] = serde_json::Value::String(value.to_string()),
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let response_channels = self.state.response_channels.lock().await;
|
|
|
|
|
for (session_id, tx) in response_channels.iter() {
|
|
|
|
|
let theme_response = crate::shared::models::BotResponse {
|
|
|
|
|
bot_id: self.bot_id.to_string(),
|
|
|
|
|
user_id: "system".to_string(),
|
|
|
|
|
session_id: session_id.clone(),
|
|
|
|
|
channel: "web".to_string(),
|
|
|
|
|
content: serde_json::to_string(&theme_data)?,
|
|
|
|
|
message_type: 2,
|
|
|
|
|
stream_token: None,
|
|
|
|
|
is_complete: true,
|
|
|
|
|
suggestions: Vec::new(),
|
|
|
|
|
context_name: None,
|
|
|
|
|
context_length: 0,
|
|
|
|
|
context_max_length: 0,
|
|
|
|
|
};
|
|
|
|
|
let _ = tx.try_send(theme_response);
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
async fn compile_tool(&self, client: &Client, file_path: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
2025-11-12 00:26:42 -03:00
|
|
|
info!("Fetching object from Drive: bucket={}, key={}", &self.bucket_name, file_path);
|
2025-11-05 21:10:03 -03:00
|
|
|
let response = match client.get_object().bucket(&self.bucket_name).key(file_path).send().await {
|
2025-11-01 09:38:15 -03:00
|
|
|
Ok(res) => {
|
2025-11-12 00:26:42 -03:00
|
|
|
info!("Successfully fetched object from Drive: bucket={}, key={}, size={}",
|
2025-11-05 21:10:03 -03:00
|
|
|
&self.bucket_name, file_path, res.content_length().unwrap_or(0));
|
2025-11-01 09:38:15 -03:00
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
2025-11-12 00:26:42 -03:00
|
|
|
log::error!("Failed to fetch object from Drive: bucket={}, key={}, error={:?}",
|
2025-11-05 21:10:03 -03:00
|
|
|
&self.bucket_name, file_path, e);
|
2025-11-01 09:38:15 -03:00
|
|
|
return Err(e.into());
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-10-30 12:35:25 -03:00
|
|
|
let bytes = response.body.collect().await?.into_bytes();
|
|
|
|
|
let source_content = String::from_utf8(bytes.to_vec())?;
|
2025-10-18 18:20:02 -03:00
|
|
|
let tool_name = file_path
|
2025-10-31 07:30:37 -03:00
|
|
|
.split('/')
|
|
|
|
|
.last()
|
2025-10-18 18:20:02 -03:00
|
|
|
.unwrap_or(file_path)
|
|
|
|
|
.strip_suffix(".bas")
|
|
|
|
|
.unwrap_or(file_path)
|
|
|
|
|
.to_string();
|
2025-11-05 21:10:03 -03:00
|
|
|
let bot_name = self.bucket_name.strip_suffix(".gbai").unwrap_or(&self.bucket_name);
|
2025-10-31 07:30:37 -03:00
|
|
|
let work_dir = format!("./work/{}.gbai/{}.gbdialog", bot_name, bot_name);
|
2025-11-08 07:04:44 -03:00
|
|
|
let state_clone = Arc::clone(&self.state);
|
|
|
|
|
let work_dir_clone = work_dir.clone();
|
|
|
|
|
let tool_name_clone = tool_name.clone();
|
|
|
|
|
let source_content_clone = source_content.clone();
|
|
|
|
|
let bot_id = self.bot_id;
|
|
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
|
std::fs::create_dir_all(&work_dir_clone)?;
|
|
|
|
|
let local_source_path = format!("{}/{}.bas", work_dir_clone, tool_name_clone);
|
|
|
|
|
std::fs::write(&local_source_path, &source_content_clone)?;
|
|
|
|
|
let mut compiler = BasicCompiler::new(state_clone, bot_id);
|
|
|
|
|
let result = compiler.compile_file(&local_source_path, &work_dir_clone)?;
|
|
|
|
|
if let Some(mcp_tool) = result.mcp_tool {
|
|
|
|
|
info!("MCP tool definition generated with {} parameters",
|
|
|
|
|
mcp_tool.input_schema.properties.len());
|
|
|
|
|
}
|
|
|
|
|
Ok::<(), Box<dyn Error + Send + Sync>>(())
|
|
|
|
|
}).await??;
|
2025-10-18 18:20:02 -03:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|