|
| 1 | +//! Mode-specific skill override helpers. |
| 2 | +
|
| 3 | +use crate::agentic::workspace::WorkspaceFileSystem; |
| 4 | +use crate::infrastructure::get_path_manager_arc; |
| 5 | +use crate::service::config::global::GlobalConfigManager; |
| 6 | +use crate::service::config::mode_config_canonicalizer::persist_mode_config_from_value; |
| 7 | +use crate::service::config::types::ModeConfig; |
| 8 | +use crate::util::errors::{BitFunError, BitFunResult}; |
| 9 | +use serde_json::{json, Map, Value}; |
| 10 | +use std::collections::{HashMap, HashSet}; |
| 11 | +use std::path::Path; |
| 12 | + |
| 13 | +const PROJECT_MODE_SKILLS_FILE_NAME: &str = "mode_skills.json"; |
| 14 | +const DISABLED_SKILLS_KEY: &str = "disabled_skills"; |
| 15 | + |
| 16 | +fn dedupe_skill_keys(keys: Vec<String>) -> Vec<String> { |
| 17 | + let mut seen = HashSet::new(); |
| 18 | + let mut normalized = Vec::new(); |
| 19 | + |
| 20 | + for key in keys { |
| 21 | + let trimmed = key.trim(); |
| 22 | + if trimmed.is_empty() { |
| 23 | + continue; |
| 24 | + } |
| 25 | + let owned = trimmed.to_string(); |
| 26 | + if seen.insert(owned.clone()) { |
| 27 | + normalized.push(owned); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + normalized |
| 32 | +} |
| 33 | + |
| 34 | +pub async fn load_disabled_user_mode_skills(mode_id: &str) -> BitFunResult<Vec<String>> { |
| 35 | + let config_service = GlobalConfigManager::get_service().await?; |
| 36 | + let stored_configs: HashMap<String, ModeConfig> = config_service |
| 37 | + .get_config(Some("ai.mode_configs")) |
| 38 | + .await |
| 39 | + .unwrap_or_default(); |
| 40 | + |
| 41 | + Ok(dedupe_skill_keys( |
| 42 | + stored_configs |
| 43 | + .get(mode_id) |
| 44 | + .map(|config| config.disabled_user_skills.clone()) |
| 45 | + .unwrap_or_default(), |
| 46 | + )) |
| 47 | +} |
| 48 | + |
| 49 | +pub async fn set_user_mode_skill_disabled( |
| 50 | + mode_id: &str, |
| 51 | + skill_key: &str, |
| 52 | + disabled: bool, |
| 53 | +) -> BitFunResult<Vec<String>> { |
| 54 | + let mut next = load_disabled_user_mode_skills(mode_id).await?; |
| 55 | + if disabled { |
| 56 | + next.push(skill_key.to_string()); |
| 57 | + next = dedupe_skill_keys(next); |
| 58 | + } else { |
| 59 | + next.retain(|value| value != skill_key); |
| 60 | + } |
| 61 | + |
| 62 | + persist_mode_config_from_value(mode_id, json!({ "disabled_user_skills": next })).await?; |
| 63 | + load_disabled_user_mode_skills(mode_id).await |
| 64 | +} |
| 65 | + |
| 66 | +pub fn project_mode_skills_path_for_remote(remote_root: &str) -> String { |
| 67 | + format!( |
| 68 | + "{}/.bitfun/config/{}", |
| 69 | + remote_root.trim_end_matches('/'), |
| 70 | + PROJECT_MODE_SKILLS_FILE_NAME |
| 71 | + ) |
| 72 | +} |
| 73 | + |
| 74 | +fn normalize_project_document_value(value: Value) -> Value { |
| 75 | + match value { |
| 76 | + Value::Object(_) => value, |
| 77 | + _ => Value::Object(Map::new()), |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +fn mode_skills_object_mut(document: &mut Value) -> BitFunResult<&mut Map<String, Value>> { |
| 82 | + if !document.is_object() { |
| 83 | + *document = Value::Object(Map::new()); |
| 84 | + } |
| 85 | + |
| 86 | + document |
| 87 | + .as_object_mut() |
| 88 | + .ok_or_else(|| BitFunError::config("Project mode skills must be a JSON object".to_string())) |
| 89 | +} |
| 90 | + |
| 91 | +fn mode_skills_object(document: &Value) -> Option<&Map<String, Value>> { |
| 92 | + document.as_object() |
| 93 | +} |
| 94 | + |
| 95 | +pub fn get_disabled_mode_skills_from_document(document: &Value, mode_id: &str) -> Vec<String> { |
| 96 | + let Some(mode_object) = mode_skills_object(document) |
| 97 | + .and_then(|map| map.get(mode_id)) |
| 98 | + .and_then(Value::as_object) |
| 99 | + else { |
| 100 | + return Vec::new(); |
| 101 | + }; |
| 102 | + |
| 103 | + let keys = mode_object |
| 104 | + .get(DISABLED_SKILLS_KEY) |
| 105 | + .cloned() |
| 106 | + .and_then(|value| serde_json::from_value::<Vec<String>>(value).ok()) |
| 107 | + .unwrap_or_default(); |
| 108 | + |
| 109 | + dedupe_skill_keys(keys) |
| 110 | +} |
| 111 | + |
| 112 | +pub fn set_mode_skill_disabled_in_document( |
| 113 | + document: &mut Value, |
| 114 | + mode_id: &str, |
| 115 | + skill_key: &str, |
| 116 | + disabled: bool, |
| 117 | +) -> BitFunResult<Vec<String>> { |
| 118 | + let mode_skills = mode_skills_object_mut(document)?; |
| 119 | + let mode_entry = mode_skills |
| 120 | + .entry(mode_id.to_string()) |
| 121 | + .or_insert_with(|| Value::Object(Map::new())); |
| 122 | + |
| 123 | + if !mode_entry.is_object() { |
| 124 | + *mode_entry = Value::Object(Map::new()); |
| 125 | + } |
| 126 | + |
| 127 | + let mode_object = mode_entry |
| 128 | + .as_object_mut() |
| 129 | + .ok_or_else(|| BitFunError::config("Mode skills entry must be a JSON object".to_string()))?; |
| 130 | + |
| 131 | + let current = mode_object |
| 132 | + .get(DISABLED_SKILLS_KEY) |
| 133 | + .cloned() |
| 134 | + .and_then(|value| serde_json::from_value::<Vec<String>>(value).ok()) |
| 135 | + .unwrap_or_default(); |
| 136 | + |
| 137 | + let mut next = dedupe_skill_keys(current); |
| 138 | + if disabled { |
| 139 | + next.push(skill_key.to_string()); |
| 140 | + next = dedupe_skill_keys(next); |
| 141 | + } else { |
| 142 | + next.retain(|value| value != skill_key); |
| 143 | + } |
| 144 | + |
| 145 | + if next.is_empty() { |
| 146 | + mode_object.remove(DISABLED_SKILLS_KEY); |
| 147 | + } else { |
| 148 | + mode_object.insert( |
| 149 | + DISABLED_SKILLS_KEY.to_string(), |
| 150 | + serde_json::to_value(&next)?, |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + if mode_object.is_empty() { |
| 155 | + mode_skills.remove(mode_id); |
| 156 | + } |
| 157 | + |
| 158 | + Ok(next) |
| 159 | +} |
| 160 | + |
| 161 | +pub async fn load_project_mode_skills_document_local( |
| 162 | + workspace_root: &Path, |
| 163 | +) -> BitFunResult<Value> { |
| 164 | + let path = get_path_manager_arc().project_mode_skills_file(workspace_root); |
| 165 | + match tokio::fs::read_to_string(&path).await { |
| 166 | + Ok(content) => Ok(normalize_project_document_value(serde_json::from_str(&content)?)), |
| 167 | + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { |
| 168 | + Ok(Value::Object(Map::new())) |
| 169 | + } |
| 170 | + Err(error) => Err(BitFunError::config(format!( |
| 171 | + "Failed to read project skill overrides file '{}': {}", |
| 172 | + path.display(), |
| 173 | + error |
| 174 | + ))), |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +pub async fn save_project_mode_skills_document_local( |
| 179 | + workspace_root: &Path, |
| 180 | + document: &Value, |
| 181 | +) -> BitFunResult<()> { |
| 182 | + let path = get_path_manager_arc().project_mode_skills_file(workspace_root); |
| 183 | + if let Some(parent) = path.parent() { |
| 184 | + tokio::fs::create_dir_all(parent).await?; |
| 185 | + } |
| 186 | + tokio::fs::write(&path, serde_json::to_vec_pretty(document)?).await?; |
| 187 | + Ok(()) |
| 188 | +} |
| 189 | + |
| 190 | +pub async fn load_disabled_mode_skills_local( |
| 191 | + workspace_root: &Path, |
| 192 | + mode_id: &str, |
| 193 | +) -> BitFunResult<Vec<String>> { |
| 194 | + let document = load_project_mode_skills_document_local(workspace_root).await?; |
| 195 | + Ok(get_disabled_mode_skills_from_document(&document, mode_id)) |
| 196 | +} |
| 197 | + |
| 198 | +pub async fn load_disabled_mode_skills_remote( |
| 199 | + fs: &dyn WorkspaceFileSystem, |
| 200 | + remote_root: &str, |
| 201 | + mode_id: &str, |
| 202 | +) -> BitFunResult<Vec<String>> { |
| 203 | + let path = project_mode_skills_path_for_remote(remote_root); |
| 204 | + let exists = fs.exists(&path).await.unwrap_or(false); |
| 205 | + if !exists { |
| 206 | + return Ok(Vec::new()); |
| 207 | + } |
| 208 | + |
| 209 | + let content = fs |
| 210 | + .read_file_text(&path) |
| 211 | + .await |
| 212 | + .map_err(|error| BitFunError::config(format!( |
| 213 | + "Failed to read remote project skill overrides: {}", |
| 214 | + error |
| 215 | + )))?; |
| 216 | + let document = normalize_project_document_value(serde_json::from_str(&content)?); |
| 217 | + Ok(get_disabled_mode_skills_from_document(&document, mode_id)) |
| 218 | +} |
0 commit comments