(
os: &Os,
session: &mut ChatSession,
name: &str,
path: &str,
include_patterns: &[String],
exclude_patterns: &[String],
index_type: &Option<Stri
| 254 | } |
| 255 | |
| 256 | async fn handle_add( |
| 257 | os: &Os, |
| 258 | session: &mut ChatSession, |
| 259 | name: &str, |
| 260 | path: &str, |
| 261 | include_patterns: &[String], |
| 262 | exclude_patterns: &[String], |
| 263 | index_type: &Option<String>, |
| 264 | ) -> OperationResult { |
| 265 | match Self::validate_and_sanitize_path(os, path) { |
| 266 | Ok(sanitized_path) => { |
| 267 | let agent = Self::get_agent(session); |
| 268 | |
| 269 | let async_knowledge_store = match KnowledgeStore::get_async_instance(os, agent).await { |
| 270 | Ok(store) => store, |
| 271 | Err(e) => return OperationResult::Error(format!("Error accessing knowledge base: {}", e)), |
| 272 | }; |
| 273 | let mut store = async_knowledge_store.lock().await; |
| 274 | |
| 275 | let include = if include_patterns.is_empty() { |
| 276 | Self::get_db_patterns(os, crate::database::settings::Setting::KnowledgeDefaultIncludePatterns) |
| 277 | } else { |
| 278 | include_patterns.to_vec() |
| 279 | }; |
| 280 | |
| 281 | let exclude = if exclude_patterns.is_empty() { |
| 282 | Self::get_db_patterns(os, crate::database::settings::Setting::KnowledgeDefaultExcludePatterns) |
| 283 | } else { |
| 284 | exclude_patterns.to_vec() |
| 285 | }; |
| 286 | |
| 287 | let embedding_type_resolved = index_type.clone().or_else(|| { |
| 288 | os.database |
| 289 | .settings |
| 290 | .get(crate::database::settings::Setting::KnowledgeIndexType) |
| 291 | .and_then(|v| v.as_str().map(|s| s.to_string())) |
| 292 | }); |
| 293 | |
| 294 | let options = crate::util::knowledge_store::AddOptions::new() |
| 295 | .with_include_patterns(include) |
| 296 | .with_exclude_patterns(exclude) |
| 297 | .with_embedding_type(embedding_type_resolved); |
| 298 | |
| 299 | match store.add(name, &sanitized_path.clone(), options).await { |
| 300 | Ok(message) => OperationResult::Info(message), |
| 301 | Err(e) => { |
| 302 | if e.contains("Invalid include pattern") || e.contains("Invalid exclude pattern") { |
| 303 | OperationResult::Error(e) |
| 304 | } else { |
| 305 | OperationResult::Error(format!("Failed to add: {}", e)) |
| 306 | } |
| 307 | }, |
| 308 | } |
| 309 | }, |
| 310 | Err(e) => OperationResult::Error(format!("Invalid path: {}", e)), |
| 311 | } |
| 312 | } |
| 313 |
nothing calls this directly
no test coverage detected