fix(tools): auto-create work directory if missing in production
All checks were successful
BotServer CI / build (push) Successful in 8m12s

Added logic to create botserver-stack/data/system/work directory
if it doesn't exist. This ensures production deployments work
without manual directory setup.

Changes:
- Added fs::create_dir_all() in use_tool.rs
- Added fs::create_dir_all() in tool_context.rs
- Logs when directory is created
- Fixes production deployment where /system/work may not exist

This ensures the tool loading works in fresh production environments
where the work folder hasn't been populated yet.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2026-02-22 12:58:58 -03:00
parent febfa2e708
commit affb702e90
2 changed files with 19 additions and 2 deletions

View file

@ -198,6 +198,7 @@ fn associate_tool_with_session(
tool_name: &str,
) -> Result<String, String> {
use crate::core::shared::models::schema::session_tool_associations;
use std::fs;
// Check if tool's .mcp.json file exists in work directory
// Use relative path from botserver binary location
@ -205,6 +206,13 @@ fn associate_tool_with_session(
.unwrap_or_else(|_| PathBuf::from("."))
.join("botserver-stack/data/system");
// Ensure work directory exists (create if not)
let work_base = gb_dir.join("work");
if !work_base.exists() {
fs::create_dir_all(&work_base).map_err(|e| format!("Failed to create work directory {:?}: {}", work_base, e))?;
info!("Created work directory at: {:?}", work_base);
}
// Get bot name to construct the path
let bot_name = get_bot_name_from_id(state, &user.bot_id)?;
let work_path = Path::new(&gb_dir)

View file

@ -1,7 +1,7 @@
use diesel::prelude::*;
use log::{debug, info, warn};
use serde_json::{json, Value};
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use uuid::Uuid;
use crate::core::shared::utils::DbPool;
@ -40,7 +40,16 @@ pub fn get_session_tools(
let gb_dir = std::env::current_dir()
.unwrap_or_else(|_| PathBuf::from("."))
.join("botserver-stack/data/system");
let work_path = Path::new(&gb_dir).join("work").join(format!("{}.gbai/{}.gbdialog", bot_name, bot_name));
// Ensure work directory exists (create if not)
let work_base = gb_dir.join("work");
if !work_base.exists() {
std::fs::create_dir_all(&work_base)
.map_err(|e| format!("Failed to create work directory {:?}: {}", work_base, e))?;
info!("Created work directory at: {:?}", work_base);
}
let work_path = work_base.join(format!("{}.gbai/{}.gbdialog", bot_name, bot_name));
info!("Loading {} tools for session {} from {:?}", tool_names.len(), session_id, work_path);