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