style: Applied consistent formatting to agent changes

This commit is contained in:
2025-10-10 14:48:10 -06:00
parent 4bf338f91a
commit f822546971
3 changed files with 82 additions and 77 deletions
+41 -39
View File
@@ -7,9 +7,9 @@ use crate::{
use anyhow::{Context, Result};
use inquire::{validator::Validation, Text};
use std::{fs::read_to_string, path::Path};
use rust_embed::Embed;
use serde::{Deserialize, Serialize};
use std::{fs::read_to_string, path::Path};
const DEFAULT_AGENT_NAME: &str = "rag";
@@ -33,34 +33,36 @@ pub struct Agent {
}
impl Agent {
pub fn install_builtin_agents() -> Result<()> {
info!("Installing built-in agents in {}", Config::agents_data_dir().display());
pub fn install_builtin_agents() -> Result<()> {
info!(
"Installing built-in agents in {}",
Config::agents_data_dir().display()
);
for file in AgentAssets::iter() {
debug!("Processing agent file: {}", file.as_ref());
for file in AgentAssets::iter() {
debug!("Processing agent file: {}", file.as_ref());
let embedded_file = AgentAssets::get(&file).ok_or_else(|| {
anyhow!(
"Failed to load embedded agent file: {}",
file.as_ref()
)
})?;
let content = unsafe { std::str::from_utf8_unchecked(&embedded_file.data) };
let file_path = Config::agents_data_dir().join(file.as_ref());
let embedded_file = AgentAssets::get(&file)
.ok_or_else(|| anyhow!("Failed to load embedded agent file: {}", file.as_ref()))?;
let content = unsafe { std::str::from_utf8_unchecked(&embedded_file.data) };
let file_path = Config::agents_data_dir().join(file.as_ref());
if file_path.exists() {
debug!("Agent file already exists, skipping: {}", file_path.display());
continue;
}
if file_path.exists() {
debug!(
"Agent file already exists, skipping: {}",
file_path.display()
);
continue;
}
ensure_parent_exists(&file_path)?;
info!("Creating agent file: {}", file_path.display());
let mut agent_file = File::create(&file_path)?;
agent_file.write_all(content.as_bytes())?;
}
ensure_parent_exists(&file_path)?;
info!("Creating agent file: {}", file_path.display());
let mut agent_file = File::create(&file_path)?;
agent_file.write_all(content.as_bytes())?;
}
Ok(())
}
Ok(())
}
pub async fn init(
config: &GlobalConfig,
@@ -563,23 +565,23 @@ pub struct AgentVariable {
}
pub fn list_agents() -> Vec<String> {
let agents_data_dir = Config::agents_data_dir();
if !agents_data_dir.exists() {
return vec![];
}
let agents_data_dir = Config::agents_data_dir();
if !agents_data_dir.exists() {
return vec![];
}
let mut agents = Vec::new();
if let Ok(entries) = read_dir(agents_data_dir) {
for entry in entries.flatten() {
if entry.path().is_dir() {
if let Some(name) = entry.file_name().to_str() {
agents.push(name.to_string());
}
}
}
}
let mut agents = Vec::new();
if let Ok(entries) = read_dir(agents_data_dir) {
for entry in entries.flatten() {
if entry.path().is_dir() {
if let Some(name) = entry.file_name().to_str() {
agents.push(name.to_string());
}
}
}
}
agents
agents
}
pub fn complete_agent_variables(agent_name: &str) -> Vec<(String, Option<String>)> {
+1 -1
View File
@@ -300,7 +300,7 @@ impl Config {
Self::load_from_file(&config_path)?
};
Agent::install_builtin_agents()?;
Agent::install_builtin_agents()?;
config.working_mode = working_mode;
config.info_flag = info_flag;
+40 -37
View File
@@ -3,6 +3,7 @@ use crate::{
utils::*,
};
use crate::config::ensure_parent_exists;
use crate::mcp::{MCP_INVOKE_META_FUNCTION_NAME_PREFIX, MCP_LIST_META_FUNCTION_NAME_PREFIX};
use crate::parsers::{bash, python};
use anyhow::{anyhow, bail, Context, Result};
@@ -20,7 +21,6 @@ use std::{
path::{Path, PathBuf},
};
use strum_macros::AsRefStr;
use crate::config::ensure_parent_exists;
#[derive(Embed)]
#[folder = "assets/functions/"]
@@ -119,41 +119,44 @@ pub struct Functions {
}
impl Functions {
fn install_global_tools() -> Result<()> {
info!("Installing global built-in functions in {}", Config::functions_dir().display());
fn install_global_tools() -> Result<()> {
info!(
"Installing global built-in functions in {}",
Config::functions_dir().display()
);
for file in FunctionAssets::iter() {
debug!("Processing function file: {}", file.as_ref());
if file.as_ref().starts_with("scripts/") {
debug!("Skipping script file: {}", file.as_ref());
continue;
}
for file in FunctionAssets::iter() {
debug!("Processing function file: {}", file.as_ref());
if file.as_ref().starts_with("scripts/") {
debug!("Skipping script file: {}", file.as_ref());
continue;
}
let embedded_file = FunctionAssets::get(&file).ok_or_else(|| {
anyhow!(
"Failed to load embedded function file: {}",
file.as_ref()
)
})?;
let content = unsafe { std::str::from_utf8_unchecked(&embedded_file.data) };
let file_path = Config::functions_dir().join(file.as_ref());
let embedded_file = FunctionAssets::get(&file).ok_or_else(|| {
anyhow!("Failed to load embedded function file: {}", file.as_ref())
})?;
let content = unsafe { std::str::from_utf8_unchecked(&embedded_file.data) };
let file_path = Config::functions_dir().join(file.as_ref());
if file_path.exists() {
debug!("Function file already exists, skipping: {}", file_path.display());
continue;
}
if file_path.exists() {
debug!(
"Function file already exists, skipping: {}",
file_path.display()
);
continue;
}
ensure_parent_exists(&file_path)?;
info!("Creating function file: {}", file_path.display());
let mut function_file = File::create(&file_path)?;
function_file.write_all(content.as_bytes())?;
}
ensure_parent_exists(&file_path)?;
info!("Creating function file: {}", file_path.display());
let mut function_file = File::create(&file_path)?;
function_file.write_all(content.as_bytes())?;
}
Ok(())
}
Ok(())
}
pub fn init() -> Result<Self> {
Self::install_global_tools()?;
Self::install_global_tools()?;
info!(
"Initializing global functions from {}",
Config::global_tools_file().display()
@@ -175,7 +178,7 @@ impl Functions {
}
pub fn init_agent(name: &str, global_tools: &[String]) -> Result<Self> {
Self::install_global_tools()?;
Self::install_global_tools()?;
let global_tools_declarations = if !global_tools.is_empty() {
let enabled_tools = global_tools.join("\n");
info!("Loading global tools for agent: {name}: {enabled_tools}");
@@ -526,13 +529,13 @@ impl Functions {
language.to_cmd()
)
}
Language::Python => {
let executable_path = which::which("python")
.or_else(|_| which::which("python3"))
.map_err(|_| anyhow!("Python executable not found in PATH"))?;
let canonicalized_path = fs::canonicalize(&executable_path)?;
canonicalized_path.to_string_lossy().into_owned()
}
Language::Python => {
let executable_path = which::which("python")
.or_else(|_| which::which("python3"))
.map_err(|_| anyhow!("Python executable not found in PATH"))?;
let canonicalized_path = fs::canonicalize(&executable_path)?;
canonicalized_path.to_string_lossy().into_owned()
}
_ => bail!("Unsupported language: {}", language.as_ref()),
};
let bin_dir = binary_file