feat: core agent tools (fs, search, shell)

This commit is contained in:
Dave
2025-12-24 16:59:14 +00:00
parent 54810631be
commit 76e03bc1a2
19 changed files with 825 additions and 52 deletions

View File

@@ -0,0 +1,133 @@
use crate::state::SessionState;
use serde::Serialize;
use std::fs;
use std::path::PathBuf;
use tauri::State;
// -----------------------------------------------------------------------------
// Helper Functions
// -----------------------------------------------------------------------------
/// Resolves a relative path against the active project root.
/// Returns error if no project is open or if path attempts traversal (..).
fn resolve_path(state: &State<'_, SessionState>, relative_path: &str) -> Result<PathBuf, String> {
let root_guard = state.project_root.lock().map_err(|e| e.to_string())?;
let root = root_guard
.as_ref()
.ok_or_else(|| "No project is currently open.".to_string())?;
// specific check for traversal
if relative_path.contains("..") {
return Err("Security Violation: Directory traversal ('..') is not allowed.".to_string());
}
// Join path
let full_path = root.join(relative_path);
Ok(full_path)
}
// -----------------------------------------------------------------------------
// Commands
// -----------------------------------------------------------------------------
#[tauri::command]
pub async fn open_project(path: String, state: State<'_, SessionState>) -> Result<String, String> {
let p = PathBuf::from(&path);
// Validate path existence in blocking thread
let p_clone = p.clone();
tauri::async_runtime::spawn_blocking(move || {
if !p_clone.exists() {
return Err(format!("Path does not exist: {}", p_clone.display()));
}
if !p_clone.is_dir() {
return Err(format!("Path is not a directory: {}", p_clone.display()));
}
Ok(())
})
.await
.map_err(|e| format!("Task failed: {}", e))??;
let mut root = state.project_root.lock().map_err(|e| e.to_string())?;
*root = Some(p.clone());
println!("Project opened: {:?}", p);
Ok(path)
}
#[tauri::command]
pub async fn read_file(path: String, state: State<'_, SessionState>) -> Result<String, String> {
let full_path = resolve_path(&state, &path)?;
tauri::async_runtime::spawn_blocking(move || {
fs::read_to_string(&full_path).map_err(|e| format!("Failed to read file: {}", e))
})
.await
.map_err(|e| format!("Task failed: {}", e))?
}
#[tauri::command]
pub async fn write_file(
path: String,
content: String,
state: State<'_, SessionState>,
) -> Result<(), String> {
let full_path = resolve_path(&state, &path)?;
tauri::async_runtime::spawn_blocking(move || {
// Ensure parent directory exists
if let Some(parent) = full_path.parent() {
fs::create_dir_all(parent)
.map_err(|e| format!("Failed to create directories: {}", e))?;
}
fs::write(&full_path, content).map_err(|e| format!("Failed to write file: {}", e))
})
.await
.map_err(|e| format!("Task failed: {}", e))?
}
#[derive(Serialize)]
pub struct FileEntry {
name: String,
kind: String, // "file" | "dir"
}
#[tauri::command]
pub async fn list_directory(
path: String,
state: State<'_, SessionState>,
) -> Result<Vec<FileEntry>, String> {
let full_path = resolve_path(&state, &path)?;
tauri::async_runtime::spawn_blocking(move || {
let entries = fs::read_dir(&full_path).map_err(|e| format!("Failed to read dir: {}", e))?;
let mut result = Vec::new();
for entry in entries {
let entry = entry.map_err(|e| e.to_string())?;
let ft = entry.file_type().map_err(|e| e.to_string())?;
let name = entry.file_name().to_string_lossy().to_string();
result.push(FileEntry {
name,
kind: if ft.is_dir() {
"dir".to_string()
} else {
"file".to_string()
},
});
}
// Sort: directories first, then files
result.sort_by(|a, b| match (a.kind.as_str(), b.kind.as_str()) {
("dir", "file") => std::cmp::Ordering::Less,
("file", "dir") => std::cmp::Ordering::Greater,
_ => a.name.cmp(&b.name),
});
Ok(result)
})
.await
.map_err(|e| format!("Task failed: {}", e))?
}

View File

@@ -0,0 +1,3 @@
pub mod fs;
pub mod search;
pub mod shell;

View File

@@ -0,0 +1,82 @@
use crate::state::SessionState;
use ignore::WalkBuilder;
use serde::Serialize;
use std::fs;
use std::path::PathBuf;
use tauri::State;
// -----------------------------------------------------------------------------
// Helper Functions
// -----------------------------------------------------------------------------
/// Helper to get the root path (cloned) without joining
fn get_project_root(state: &State<'_, SessionState>) -> Result<PathBuf, String> {
let root_guard = state.project_root.lock().map_err(|e| e.to_string())?;
let root = root_guard
.as_ref()
.ok_or_else(|| "No project is currently open.".to_string())?;
Ok(root.clone())
}
// -----------------------------------------------------------------------------
// Commands
// -----------------------------------------------------------------------------
#[derive(Serialize)]
pub struct SearchResult {
path: String, // Relative path
matches: usize,
}
#[tauri::command]
pub async fn search_files(
query: String,
state: State<'_, SessionState>,
) -> Result<Vec<SearchResult>, String> {
let root = get_project_root(&state)?;
let root_clone = root.clone();
// Run computationally expensive search on a blocking thread
let results = tauri::async_runtime::spawn_blocking(move || {
let mut matches = Vec::new();
// default to respecting .gitignore
let walker = WalkBuilder::new(&root_clone).git_ignore(true).build();
for result in walker {
match result {
Ok(entry) => {
if !entry.file_type().map(|ft| ft.is_file()).unwrap_or(false) {
continue;
}
let path = entry.path();
// Try to read file
// Note: This is a naive implementation reading whole files into memory.
// For production, we should stream/buffer reads or use grep-searcher.
if let Ok(content) = fs::read_to_string(path) {
// Simple substring search (case-sensitive)
if content.contains(&query) {
// Compute relative path for display
let relative = path
.strip_prefix(&root_clone)
.unwrap_or(path)
.to_string_lossy()
.to_string();
matches.push(SearchResult {
path: relative,
matches: 1, // Simplified count for now
});
}
}
}
Err(err) => eprintln!("Error walking dir: {}", err),
}
}
matches
})
.await
.map_err(|e| format!("Search task failed: {}", e))?;
Ok(results)
}

View File

@@ -0,0 +1,76 @@
use crate::state::SessionState;
use serde::Serialize;
use std::path::PathBuf;
use std::process::Command;
use tauri::State;
// -----------------------------------------------------------------------------
// Helper Functions
// -----------------------------------------------------------------------------
/// Helper to get the root path (cloned) without joining
fn get_project_root(state: &State<'_, SessionState>) -> Result<PathBuf, String> {
let root_guard = state.project_root.lock().map_err(|e| e.to_string())?;
let root = root_guard
.as_ref()
.ok_or_else(|| "No project is currently open.".to_string())?;
Ok(root.clone())
}
// -----------------------------------------------------------------------------
// Commands
// -----------------------------------------------------------------------------
#[derive(Serialize)]
pub struct CommandOutput {
stdout: String,
stderr: String,
exit_code: i32,
}
#[tauri::command]
pub async fn exec_shell(
command: String,
args: Vec<String>,
state: State<'_, SessionState>,
) -> Result<CommandOutput, String> {
let root = get_project_root(&state)?;
// Security Allowlist
let allowed_commands = [
"git", "cargo", "npm", "yarn", "pnpm", "node", "bun", "ls", "find", "grep", "mkdir", "rm",
"mv", "cp", "touch", "rustc", "rustfmt",
];
if !allowed_commands.contains(&command.as_str()) {
return Err(format!("Command '{}' is not in the allowlist.", command));
}
// Execute command asynchronously
// Note: This blocks the async runtime thread unless we use tokio::process::Command,
// but tauri::command async wrapper handles offloading reasonably well.
// However, specifically for Tauri, standard Command in an async function runs on the thread pool.
// Ideally we'd use tokio::process::Command but we need to add 'tokio' with 'process' feature.
// For now, standard Command inside tauri async command (which runs on a separate thread) is acceptable
// or we can explicitly spawn_blocking.
//
// Actually, tauri::command async functions run on the tokio runtime.
// Calling std::process::Command::output() blocks the thread.
// We should use tauri::async_runtime::spawn_blocking.
let output = tauri::async_runtime::spawn_blocking(move || {
Command::new(&command)
.args(&args)
.current_dir(root)
.output()
})
.await
.map_err(|e| format!("Task join error: {}", e))?
.map_err(|e| format!("Failed to execute command: {}", e))?;
Ok(CommandOutput {
stdout: String::from_utf8_lossy(&output.stdout).to_string(),
stderr: String::from_utf8_lossy(&output.stderr).to_string(),
exit_code: output.status.code().unwrap_or(-1),
})
}