use serde::Serialize;
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::Instant;
use tauri::{AppHandle, State};
use crate::core::error::AppError;
use crate::core::scenario_service;
use crate::core::scenario_service::sync_scenario_skills;
use crate::core::skill_store::SkillStore;
use crate::core::sync_engine;
use crate::core::timing::should_log_first_or_slow;
use crate::core::tool_adapters::{self, CustomToolDef, ToolCategory};
use crate::core::tool_service::{
self, get_custom_tool_paths, get_custom_tool_project_paths, get_custom_tools,
get_disabled_tools, get_tool_order, normalize_project_relative_skills_dir_input,
normalize_skills_dir_input, set_custom_tool_paths, set_custom_tool_project_paths,
set_custom_tools, set_disabled_tools, set_tool_order, ToolInfo,
};
#[derive(Debug, Serialize)]
pub struct ToolInfoDto {
pub key: String,
pub display_name: String,
pub installed: bool,
pub skills_dir: String,
pub enabled: bool,
pub is_custom: bool,
pub has_path_override: bool,
pub project_relative_skills_dir: Option<String>,
pub has_project_path_override: bool,
pub category: ToolCategory,
}
fn sync_active_scenario_to_tool(store: &SkillStore, tool_key: &str) {
scenario_service::sync_active_scenario_to_tool(store, tool_key)
}
fn unsync_all_for_tool(store: &SkillStore, tool_key: &str) {
let targets = store.get_all_targets().unwrap_or_default();
for target in targets.iter().filter(|t| t.tool == tool_key) {
let still_referenced = targets
.iter()
.any(|other| other.tool != tool_key && other.target_path == target.target_path);
if !still_referenced {
sync_engine::remove_recorded_target_or_warn(
&PathBuf::from(&target.target_path),
&target.mode,
);
}
store.delete_target(&target.skill_id, tool_key).ok();
}
}
fn reconcile_tool_sync_after_path_change(store: &SkillStore, tool_key: &str) {
unsync_all_for_tool(store, tool_key);
let disabled = get_disabled_tools(store);
if !disabled.contains(&tool_key.to_string()) {
sync_active_scenario_to_tool(store, tool_key);
}
}
static GET_TOOL_STATUS_FIRST_CALL: AtomicBool = AtomicBool::new(true);
#[tauri::command]
pub async fn get_tool_status(
store: State<'_, Arc<SkillStore>>,
) -> Result<Vec<ToolInfoDto>, AppError> {
let store = store.inner().clone();
tauri::async_runtime::spawn_blocking(move || {
let start = Instant::now();
let infos = tool_service::list_tool_info(&store);
let count = infos.len();
let result: Vec<ToolInfoDto> = infos
.into_iter()
.map(|info: ToolInfo| ToolInfoDto {
key: info.key,
display_name: info.display_name,
installed: info.installed,
skills_dir: info.skills_dir,
enabled: info.enabled,
is_custom: info.is_custom,
has_path_override: info.has_path_override,
project_relative_skills_dir: info.project_relative_skills_dir,
has_project_path_override: info.has_project_path_override,
category: info.category,
})
.collect();
let elapsed_ms = start.elapsed().as_millis();
if should_log_first_or_slow(&GET_TOOL_STATUS_FIRST_CALL, elapsed_ms, 100) {
log::info!("get_tool_status: {count} tools in {elapsed_ms} ms");
}
Ok(result)
})
.await?
}
fn refresh_tray_menu_best_effort(app: &AppHandle) {
if let Err(err) = crate::refresh_tray_menu(app) {
log::warn!("Failed to refresh tray menu after tool mutation: {err}");
}
}
#[tauri::command]
pub async fn set_tool_enabled(
app: AppHandle,
key: String,
enabled: bool,
store: State<'_, Arc<SkillStore>>,
) -> Result<(), AppError> {
let store = store.inner().clone();
let result = tauri::async_runtime::spawn_blocking(move || {
set_tool_enabled_internal(&store, &key, enabled)
})
.await?;
if result.is_ok() {
refresh_tray_menu_best_effort(&app);
}
result
}
pub fn set_tool_enabled_internal(
store: &SkillStore,
key: &str,
enabled: bool,
) -> Result<(), AppError> {
if tool_adapters::find_adapter_with_store(store, key).is_none() {
return Err(AppError::not_found(format!("Unknown agent: {key}")));
}
let mut disabled = get_disabled_tools(store);
if enabled {
disabled.retain(|item| item != key);
set_disabled_tools(store, &disabled)?;
sync_active_scenario_to_tool(store, key);
Ok(())
} else {
if !disabled.iter().any(|item| item == key) {
disabled.push(key.to_string());
}
unsync_all_for_tool(store, key);
set_disabled_tools(store, &disabled)
}
}
#[tauri::command]
pub async fn set_all_tools_enabled(
app: AppHandle,
enabled: bool,
store: State<'_, Arc<SkillStore>>,
) -> Result<(), AppError> {
let store = store.inner().clone();
let result = tauri::async_runtime::spawn_blocking(move || {
if enabled {
set_disabled_tools(&store, &[])?;
if let Ok(Some(active_id)) = store.get_active_scenario_id() {
match sync_scenario_skills(&store, &active_id, false) {
Ok(refusals) => {
for refusal in refusals {
log::warn!("enable-all-tools sync skipped a target: {refusal}");
}
}
Err(e) => log::warn!("enable-all-tools sync failed: {e}"),
}
}
Ok(())
} else {
let adapters = tool_adapters::all_tool_adapters(&store);
let all_keys: Vec<String> = adapters.iter().map(|a| a.key.clone()).collect();
for adapter in &adapters {
unsync_all_for_tool(&store, &adapter.key);
}
set_disabled_tools(&store, &all_keys)
}
})
.await?;
if result.is_ok() {
refresh_tray_menu_best_effort(&app);
}
result
}
#[tauri::command]
pub async fn get_tool_order_cmd(
store: State<'_, Arc<SkillStore>>,
) -> Result<Vec<String>, AppError> {
let store = store.inner().clone();
tauri::async_runtime::spawn_blocking(move || Ok(get_tool_order(&store))).await?
}
#[tauri::command]
pub async fn set_tool_order_cmd(
order: Vec<String>,
store: State<'_, Arc<SkillStore>>,
) -> Result<(), AppError> {
let store = store.inner().clone();
tauri::async_runtime::spawn_blocking(move || set_tool_order(&store, &order)).await?
}
fn is_builtin_key(key: &str) -> bool {
tool_adapters::default_tool_adapters()
.iter()
.any(|a| a.key == key)
}
pub(crate) fn apply_tool_skills_dir(
store: &SkillStore,
key: &str,
path: &str,
) -> Result<(), AppError> {
let key = key.trim().to_string();
let path = normalize_skills_dir_input(path)?;
if key.is_empty() || path.is_empty() {
return Err(AppError::invalid_input("Key and path are required"));
}
let old_adapter = tool_adapters::find_adapter_with_store(store, &key)
.ok_or_else(|| AppError::not_found(format!("Unknown tool: {key}")))?;
let old_skills_dir = old_adapter.skills_dir();
let mut customs = get_custom_tools(store);
match customs.iter_mut().find(|c| c.key == key) {
Some(custom) if !is_builtin_key(&key) => {
custom.skills_dir = path;
set_custom_tools(store, &customs)?;
}
_ => {
let mut paths = get_custom_tool_paths(store);
paths.insert(key.clone(), path);
set_custom_tool_paths(store, &paths)?;
}
}
let new_adapter = tool_adapters::find_adapter_with_store(store, &key)
.ok_or_else(|| AppError::not_found(format!("Unknown tool: {key}")))?;
if old_skills_dir != new_adapter.skills_dir() {
reconcile_tool_sync_after_path_change(store, &key);
}
Ok(())
}
#[tauri::command]
pub async fn set_custom_tool_path(
key: String,
path: String,
store: State<'_, Arc<SkillStore>>,
) -> Result<(), AppError> {
let store = store.inner().clone();
tauri::async_runtime::spawn_blocking(move || apply_tool_skills_dir(&store, &key, &path)).await?
}
#[tauri::command]
pub async fn reset_custom_tool_path(
key: String,
store: State<'_, Arc<SkillStore>>,
) -> Result<(), AppError> {
let store = store.inner().clone();
tauri::async_runtime::spawn_blocking(move || {
let old_adapter = tool_adapters::find_adapter_with_store(&store, &key)
.ok_or_else(|| AppError::not_found(format!("Unknown tool: {key}")))?;
let old_skills_dir = old_adapter.skills_dir();
let mut paths = get_custom_tool_paths(&store);
paths.remove(&key);
set_custom_tool_paths(&store, &paths)?;
let new_adapter = tool_adapters::find_adapter_with_store(&store, &key)
.ok_or_else(|| AppError::not_found(format!("Unknown tool: {key}")))?;
if old_skills_dir != new_adapter.skills_dir() {
reconcile_tool_sync_after_path_change(&store, &key);
}
Ok(())
})
.await?
}
pub(crate) fn apply_tool_project_skills_dir(
store: &SkillStore,
key: &str,
project_relative_skills_dir: Option<&str>,
) -> Result<(), AppError> {
let key = key.trim().to_string();
if key.is_empty() {
return Err(AppError::invalid_input("Key is required"));
}
let normalized = normalize_project_relative_skills_dir_input(
project_relative_skills_dir.unwrap_or_default(),
)?;
let default_project_path = match tool_adapters::default_tool_adapters()
.into_iter()
.find(|a| a.key == key)
.map(|a| a.project_relative_skills_dir().to_string())
{
Some(default_project_path) => default_project_path,
None => {
let mut customs = get_custom_tools(store);
let custom = customs
.iter_mut()
.find(|c| c.key == key)
.ok_or_else(|| AppError::not_found(format!("Unknown tool: {key}")))?;
custom.project_relative_skills_dir = normalized;
return set_custom_tools(store, &customs);
}
};
let mut project_paths = get_custom_tool_project_paths(store);
match normalized {
Some(path) if path != default_project_path => {
project_paths.insert(key, path);
}
_ => {
project_paths.remove(&key);
}
}
set_custom_tool_project_paths(store, &project_paths)
}
#[tauri::command]
pub async fn set_custom_tool_project_path(
key: String,
project_relative_skills_dir: Option<String>,
store: State<'_, Arc<SkillStore>>,
) -> Result<(), AppError> {
let store = store.inner().clone();
tauri::async_runtime::spawn_blocking(move || {
apply_tool_project_skills_dir(&store, &key, project_relative_skills_dir.as_deref())
})
.await?
}
#[tauri::command]
pub async fn reset_custom_tool_project_path(
key: String,
store: State<'_, Arc<SkillStore>>,
) -> Result<(), AppError> {
let store = store.inner().clone();
tauri::async_runtime::spawn_blocking(move || {
let key = key.trim().to_string();
if key.is_empty() {
return Err(AppError::invalid_input("Key is required"));
}
if tool_adapters::find_adapter_with_store(&store, &key).is_none() {
return Err(AppError::not_found(format!("Unknown tool: {key}")));
}
let mut project_paths = get_custom_tool_project_paths(&store);
if project_paths.remove(&key).is_some() {
set_custom_tool_project_paths(&store, &project_paths)?;
}
Ok(())
})
.await?
}
#[tauri::command]
pub async fn add_custom_tool(
key: String,
display_name: String,
skills_dir: String,
project_relative_skills_dir: Option<String>,
store: State<'_, Arc<SkillStore>>,
) -> Result<(), AppError> {
let store = store.inner().clone();
tauri::async_runtime::spawn_blocking(move || {
add_custom_tool_internal(
&store,
&key,
&display_name,
&skills_dir,
project_relative_skills_dir.as_deref(),
)
})
.await?
}
pub fn add_custom_tool_internal(
store: &SkillStore,
key: &str,
display_name: &str,
skills_dir: &str,
project_relative_skills_dir: Option<&str>,
) -> Result<(), AppError> {
let key = key.trim().to_string();
let display_name = display_name.trim().to_string();
let skills_dir = normalize_skills_dir_input(skills_dir)?;
let project_relative_skills_dir = normalize_project_relative_skills_dir_input(
project_relative_skills_dir.unwrap_or_default(),
)?;
if key.is_empty() || display_name.is_empty() || skills_dir.is_empty() {
return Err(AppError::invalid_input(
"Agent key, name and skills path are required",
));
}
let all = tool_adapters::all_tool_adapters(store);
if all.iter().any(|a| a.key == key) {
return Err(AppError::invalid_input(format!(
"Agent key \"{key}\" already exists"
)));
}
let mut customs = get_custom_tools(store);
customs.push(CustomToolDef {
key: key.clone(),
display_name,
skills_dir,
project_relative_skills_dir,
category: Default::default(),
});
set_custom_tools(store, &customs)?;
reconcile_tool_sync_after_path_change(store, &key);
Ok(())
}
#[tauri::command]
pub async fn remove_custom_tool(
key: String,
store: State<'_, Arc<SkillStore>>,
) -> Result<(), AppError> {
let store = store.inner().clone();
tauri::async_runtime::spawn_blocking(move || {
unsync_all_for_tool(&store, &key);
let mut customs = get_custom_tools(&store);
customs.retain(|c| c.key != key);
set_custom_tools(&store, &customs)?;
let mut custom_paths = get_custom_tool_paths(&store);
custom_paths.remove(&key);
set_custom_tool_paths(&store, &custom_paths)?;
let mut disabled = get_disabled_tools(&store);
disabled.retain(|k| k != &key);
set_disabled_tools(&store, &disabled)
})
.await?
}
pub fn migrate_legacy_tool_keys(store: &SkillStore) -> Result<(), AppError> {
tool_service::migrate_legacy_tool_keys(store)
}
#[cfg(test)]
mod tests;