use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolConfig {
#[serde(default)]
pub modules: ModuleConfig,
#[serde(default)]
pub tools: ToolFilterConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModuleConfig {
#[serde(default = "yes")]
pub filesystem: bool,
#[serde(default = "yes")]
pub diagnostics: bool,
#[serde(default = "yes")]
pub silent: bool,
#[serde(default = "yes")]
pub time: bool,
#[serde(default = "yes")]
pub network: bool,
#[serde(default = "yes")]
pub context: bool,
#[serde(default = "yes")]
pub git: bool,
#[serde(default = "yes")]
pub input: bool,
#[serde(default = "yes")]
pub gitent: bool,
#[serde(default = "yes")]
pub clipboard: bool,
#[serde(default = "yes")]
pub transform: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ToolFilterConfig {
#[serde(default)]
pub enabled: Vec<String>,
#[serde(default)]
pub disabled: Vec<String>,
}
fn yes() -> bool {
true
}
impl Default for ToolConfig {
fn default() -> Self {
Self {
modules: ModuleConfig::default(),
tools: ToolFilterConfig::default(),
}
}
}
impl Default for ModuleConfig {
fn default() -> Self {
Self {
filesystem: true,
diagnostics: true,
silent: true,
time: true,
network: true,
context: true,
git: true,
input: true,
gitent: true,
clipboard: true,
transform: true,
}
}
}
impl ModuleConfig {
pub fn all_disabled() -> Self {
Self {
filesystem: false,
diagnostics: false,
silent: false,
time: false,
network: false,
context: false,
git: false,
input: false,
gitent: false,
clipboard: false,
transform: false,
}
}
pub fn set(&mut self, name: &str, enabled: bool) {
match name {
"filesystem" | "fs" => self.filesystem = enabled,
"diagnostics" | "diag" => self.diagnostics = enabled,
"silent" => self.silent = enabled,
"time" => self.time = enabled,
"network" | "net" => self.network = enabled,
"context" | "ctx" => self.context = enabled,
"git" => self.git = enabled,
"input" => self.input = enabled,
"gitent" => self.gitent = enabled,
"clipboard" | "clip" => self.clipboard = enabled,
"transform" => self.transform = enabled,
_ => eprintln!("Warning: unknown module '{}', ignoring", name),
}
}
pub fn is_enabled(&self, name: &str) -> bool {
match name {
"filesystem" => self.filesystem,
"diagnostics" => self.diagnostics,
"silent" => self.silent,
"time" => self.time,
"network" => self.network,
"context" => self.context,
"git" => self.git,
"input" => self.input,
"gitent" => self.gitent,
"clipboard" => self.clipboard,
"transform" => self.transform,
_ => false,
}
}
#[allow(dead_code)]
pub fn enabled_list(&self) -> Vec<&'static str> {
let mut v = Vec::new();
if self.filesystem {
v.push("filesystem");
}
if self.diagnostics {
v.push("diagnostics");
}
if self.silent {
v.push("silent");
}
if self.time {
v.push("time");
}
if self.network {
v.push("network");
}
if self.context {
v.push("context");
}
if self.git {
v.push("git");
}
if self.input {
v.push("input");
}
if self.gitent {
v.push("gitent");
}
if self.clipboard {
v.push("clipboard");
}
if self.transform {
v.push("transform");
}
v
}
}
impl ToolConfig {
pub fn only(modules: &[&str]) -> Self {
let mut config = Self {
modules: ModuleConfig::all_disabled(),
tools: ToolFilterConfig::default(),
};
for m in modules {
config.modules.set(m, true);
}
config
}
pub fn without(modules: &[&str]) -> Self {
let mut config = Self::default();
for m in modules {
config.modules.set(m, false);
}
config
}
pub fn enable_module(mut self, name: &str) -> Self {
self.modules.set(name, true);
self
}
pub fn disable_module(mut self, name: &str) -> Self {
self.modules.set(name, false);
self
}
pub fn disable_tool(mut self, name: impl Into<String>) -> Self {
let name = name.into();
if !self.tools.disabled.contains(&name) {
self.tools.disabled.push(name);
}
self
}
pub fn enable_only_tools(mut self, names: &[&str]) -> Self {
self.tools.enabled = names.iter().map(|s| s.to_string()).collect();
self
}
pub fn from_file(path: &Path) -> anyhow::Result<Self> {
let content = std::fs::read_to_string(path)?;
let config: Self = serde_json::from_str(&content)?;
Ok(config)
}
pub fn apply_cli(
&mut self,
enable_modules: Option<&str>,
disable_modules: Option<&str>,
disable_tools: Option<&str>,
) {
if let Some(modules) = enable_modules {
self.modules = ModuleConfig::all_disabled();
for m in modules.split(',').map(str::trim).filter(|s| !s.is_empty()) {
self.modules.set(m, true);
}
}
if let Some(modules) = disable_modules {
for m in modules.split(',').map(str::trim).filter(|s| !s.is_empty()) {
self.modules.set(m, false);
}
}
if let Some(tools) = disable_tools {
for t in tools.split(',').map(str::trim).filter(|s| !s.is_empty()) {
if !self.tools.disabled.contains(&t.to_string()) {
self.tools.disabled.push(t.to_string());
}
}
}
}
pub fn is_tool_enabled(&self, tool_name: &str) -> bool {
let module = tool_module(tool_name);
if !self.modules.is_enabled(module) {
return false;
}
if !self.tools.enabled.is_empty()
&& !self.tools.enabled.iter().any(|t| t == tool_name)
{
return false;
}
if self.tools.disabled.iter().any(|t| t == tool_name) {
return false;
}
true
}
pub fn generate_default() -> String {
serde_json::to_string_pretty(&Self::default()).unwrap()
}
}
pub fn is_module_compiled(name: &str) -> bool {
match name {
"filesystem" => cfg!(feature = "filesystem"),
"diagnostics" => cfg!(feature = "diagnostics"),
"silent" => cfg!(feature = "silent"),
"time" => cfg!(feature = "time"),
"network" => cfg!(feature = "network"),
"context" => cfg!(feature = "context"),
"git" => cfg!(feature = "git"),
"input" => cfg!(feature = "input"),
"gitent" => cfg!(feature = "gitent"),
"clipboard" => cfg!(feature = "clipboard"),
"transform" => cfg!(feature = "transform"),
_ => false,
}
}
pub fn tool_module(name: &str) -> &str {
if name.starts_with("fs_") {
"filesystem"
} else if name.starts_with("diagnostics_") {
"diagnostics"
} else if name.starts_with("silent_") {
"silent"
} else if name.starts_with("time_") {
"time"
} else if name.starts_with("net_") {
"network"
} else if name.starts_with("ctx_") {
"context"
} else if name.starts_with("git_") {
"git"
} else if name.starts_with("input_") {
"input"
} else if name.starts_with("gitent_") {
"gitent"
} else if name.starts_with("clip_") {
"clipboard"
} else if name.starts_with("transform_") {
"transform"
} else {
"unknown"
}
}
#[allow(dead_code)]
pub const ALL_MODULE_NAMES: &[&str] = &[
"filesystem",
"diagnostics",
"silent",
"time",
"network",
"context",
"git",
"input",
"gitent",
"clipboard",
"transform",
];
pub struct ModuleInfo {
pub name: &'static str,
pub description: &'static str,
pub tool_count: usize,
pub tools: &'static [&'static str],
}
pub fn all_module_info() -> Vec<ModuleInfo> {
vec![
ModuleInfo {
name: "filesystem",
description: "File and directory operations",
tool_count: 17,
tools: &[
"fs_read", "fs_write", "fs_move", "fs_copy", "fs_create", "fs_delete",
"fs_move_desktop", "fs_find", "fs_ld", "fs_stat", "fs_permissions",
"fs_watch", "fs_snapshot", "fs_tree", "fs_grep", "fs_tail", "fs_replace",
],
},
ModuleInfo {
name: "diagnostics",
description: "Language-agnostic error detection",
tool_count: 1,
tools: &["diagnostics_get"],
},
ModuleInfo {
name: "silent",
description: "Bash scripting and resource monitoring",
tool_count: 2,
tools: &["silent_script", "silent_resources"],
},
ModuleInfo {
name: "time",
description: "Time management, scheduling & timekeeping",
tool_count: 7,
tools: &[
"time_now", "time_sleep", "time_schedule",
"time_timezone", "time_stopwatch", "time_timer", "time_alarm",
],
},
ModuleInfo {
name: "network",
description: "HTTP requests and package queries",
tool_count: 6,
tools: &["net_fetch", "net_cargo", "net_node", "net_python", "net_apt", "net_ping"],
},
ModuleInfo {
name: "context",
description: "Token counting and cost estimation",
tool_count: 7,
tools: &[
"ctx_context", "ctx_compact", "ctx_remove", "ctx_token_count",
"ctx_memory_store", "ctx_memory_recall", "ctx_estimate_cost",
],
},
ModuleInfo {
name: "git",
description: "Complete git operations",
tool_count: 8,
tools: &[
"git_status", "git_diff", "git_commit", "git_branch",
"git_checkout", "git_blame", "git_log", "git_tag",
],
},
ModuleInfo {
name: "input",
description: "User interaction and notifications",
tool_count: 6,
tools: &[
"input_notify", "input_prompt", "input_select", "input_progress",
"input_clipboard_read", "input_clipboard_write",
],
},
ModuleInfo {
name: "gitent",
description: "Agent-centric version control tracking",
tool_count: 7,
tools: &[
"gitent_init", "gitent_status", "gitent_track", "gitent_commit",
"gitent_log", "gitent_diff", "gitent_rollback",
],
},
ModuleInfo {
name: "clipboard",
description: "Session copy/paste with tags",
tool_count: 5,
tools: &["clip_copy_file", "clip_copy", "clip_paste_file", "clip_paste", "clip_clear"],
},
ModuleInfo {
name: "transform",
description: "Text & data processing",
tool_count: 7,
tools: &[
"transform_diff", "transform_encode", "transform_hash", "transform_regex",
"transform_json", "transform_text", "transform_archive",
],
},
]
}