fix: use get_work_path() instead of get_stack_path()+data/system for work dir, add etag check for PROMPT.md downloads
All checks were successful
BotServer CI/CD / build (push) Successful in 3m37s

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2026-04-11 18:42:09 -03:00
parent a4a3837c4c
commit f4c99030aa
3 changed files with 49 additions and 41 deletions

View file

@ -144,15 +144,11 @@ fn associate_tool_with_session(
use crate::core::shared::models::schema::session_tool_associations; use crate::core::shared::models::schema::session_tool_associations;
// Check if tool's .mcp.json file exists in work directory // Check if tool's .mcp.json file exists in work directory
// Use relative path from botserver binary current directory let work_root = crate::core::shared::utils::get_work_path();
let gb_dir =
std::path::PathBuf::from(crate::core::shared::utils::get_stack_path()).join("data/system");
// Get bot name to construct the path // Get bot name to construct the path
let bot_name = get_bot_name_from_id(state, &user.bot_id)?; let bot_name = get_bot_name_from_id(state, &user.bot_id)?;
let work_path = Path::new(&gb_dir) let work_path = Path::new(&work_root).join(format!("{}.gbai/{}.gbdialog", bot_name, bot_name));
.join("work")
.join(format!("{}.gbai/{}.gbdialog", bot_name, bot_name));
let mcp_path = work_path.join(format!("{}.mcp.json", tool_name)); let mcp_path = work_path.join(format!("{}.mcp.json", tool_name));
trace!("Checking for tool .mcp.json at: {:?}", mcp_path); trace!("Checking for tool .mcp.json at: {:?}", mcp_path);

View file

@ -35,19 +35,16 @@ pub fn get_session_tools(
} }
// Build path to work/{bot_name}.gbai/{bot_name}.gbdialog directory // Build path to work/{bot_name}.gbai/{bot_name}.gbdialog directory
// Use relative path from botserver binary location let work_root = std::path::PathBuf::from(crate::core::shared::utils::get_work_path());
let gb_dir =
std::path::PathBuf::from(crate::core::shared::utils::get_stack_path()).join("data/system");
// Ensure work directory exists (create if not) // Ensure work directory exists (create if not)
let work_base = gb_dir.join("work"); if !work_root.exists() {
if !work_base.exists() { std::fs::create_dir_all(&work_root)
std::fs::create_dir_all(&work_base) .map_err(|e| format!("Failed to create work directory {:?}: {}", work_root, e))?;
.map_err(|e| format!("Failed to create work directory {:?}: {}", work_base, e))?; info!("Created work directory at: {:?}", work_root);
info!("Created work directory at: {:?}", work_base);
} }
let work_path = work_base.join(format!("{}.gbai/{}.gbdialog", bot_name, bot_name)); let work_path = work_root.join(format!("{}.gbai/{}.gbdialog", bot_name, bot_name));
info!( info!(
"Loading {} tools for session {} from {:?}", "Loading {} tools for session {} from {:?}",

View file

@ -600,35 +600,50 @@ impl DriveMonitor {
} }
if is_prompt_file { if is_prompt_file {
// Download prompt file to work directory // Check etag to avoid re-downloading unchanged prompt files
match client.get_object().bucket(&self.bucket_name).key(&path).send().await { let etag = obj.e_tag().unwrap_or_default().to_string();
Ok(response) => { let prompt_state_key = format!("__prompt__{}", path);
let bytes = response.body.collect().await?.into_bytes(); let should_download = {
let content = String::from_utf8(bytes.to_vec()) let states = self.file_states.read().await;
.map_err(|e| format!("UTF-8 error in {}: {}", path, e))?; match states.get(&prompt_state_key) {
let bot_name = self.bucket_name.strip_suffix(".gbai").unwrap_or(&self.bucket_name); Some(prev) => prev.etag != etag,
let gbot_dir = self.work_root.join(format!("{}.gbai/{}.gbot", bot_name, bot_name)); None => true,
let path_buf = PathBuf::from(&path); }
let file_name = path_buf.file_name() };
.and_then(|n| n.to_str()).unwrap_or("PROMPT.md"); if should_download {
if let Err(e) = tokio::task::spawn_blocking({ match client.get_object().bucket(&self.bucket_name).key(&path).send().await {
let gbot_dir_str = gbot_dir.to_string_lossy().to_string(); Ok(response) => {
let file_name_owned = file_name.to_string(); let bytes = response.body.collect().await?.into_bytes();
let content_owned = content.clone(); let content = String::from_utf8(bytes.to_vec())
move || { .map_err(|e| format!("UTF-8 error in {}: {}", path, e))?;
std::fs::create_dir_all(&gbot_dir_str)?; let bot_name = self.bucket_name.strip_suffix(".gbai").unwrap_or(&self.bucket_name);
std::fs::write(format!("{}/{}", gbot_dir_str, file_name_owned), &content_owned)?; let gbot_dir = self.work_root.join(format!("{}.gbai/{}.gbot", bot_name, bot_name));
Ok::<(), Box<dyn std::error::Error + Send + Sync>>(()) let path_buf = PathBuf::from(&path);
let file_name = path_buf.file_name()
.and_then(|n| n.to_str()).unwrap_or("PROMPT.md");
if let Err(e) = tokio::task::spawn_blocking({
let gbot_dir_str = gbot_dir.to_string_lossy().to_string();
let file_name_owned = file_name.to_string();
let content_owned = content.clone();
move || {
std::fs::create_dir_all(&gbot_dir_str)?;
std::fs::write(format!("{}/{}", gbot_dir_str, file_name_owned), &content_owned)?;
Ok::<(), Box<dyn std::error::Error + Send + Sync>>(())
}
}).await {
log::error!("Failed to save prompt file: {}", e);
} else {
log::info!("Downloaded prompt file {} to work directory", path);
} }
}).await { }
log::error!("Failed to save prompt file: {}", e); Err(e) => {
} else { log::error!("Failed to download prompt file {}: {}", path, e);
log::info!("Downloaded prompt file {} to work directory", path);
} }
} }
Err(e) => { let mut states = self.file_states.write().await;
log::error!("Failed to download prompt file {}: {}", path, e); states.insert(prompt_state_key, FileState { etag, indexed: false });
} } else {
trace!("Prompt file {} unchanged (etag match), skipping download", path);
} }
continue; continue;
} }