| 1 | use super::*; |
| 2 | use crate::tui::plugin_suggestions::ExternalCatalogChange; |
| 3 | |
| 4 | fn skill_delta(before: &[(String, String)], after: &[(String, String)]) -> String { |
| 5 | let names = |skills: &[(String, String)]| { |
| 6 | skills |
| 7 | .iter() |
| 8 | .map(|(name, _)| name.clone()) |
| 9 | .collect::<std::collections::BTreeSet<_>>() |
| 10 | }; |
| 11 | let (before, after) = (names(before), names(after)); |
| 12 | let added = after.difference(&before).cloned().collect::<Vec<_>>(); |
| 13 | let removed = before.difference(&after).cloned().collect::<Vec<_>>(); |
| 14 | let mut parts = Vec::new(); |
| 15 | if !added.is_empty() { |
| 16 | parts.push(format!("skills added: {}", added.join(", "))); |
| 17 | } |
| 18 | if !removed.is_empty() { |
| 19 | parts.push(format!("skills removed: {}", removed.join(", "))); |
| 20 | } |
| 21 | parts.join("; ") |
| 22 | } |
| 23 | |
| 24 | pub(crate) async fn apply_external_catalog_change( |
| 25 | terminal: &mut AppTerminal, |
| 26 | app: &mut App, |
| 27 | engine_handle: &mut EngineHandle, |
| 28 | config: &mut Config, |
| 29 | change: ExternalCatalogChange, |
| 30 | ) -> Result<()> { |
| 31 | app.mark_external_catalog_applied(); |
| 32 | if change.plugins { |
| 33 | let before = app.plugin_registry.clone(); |
| 34 | let skills_before = app.cached_skills.clone(); |
| 35 | let workspace = app.workspace.clone(); |
| 36 | app.plugin_registry = before.rediscover_for_workspace(&workspace); |
| 37 | app.refresh_skill_cache(); |
| 38 | let mut message = String::from("Plugins changed outside this session and were applied"); |
| 39 | let delta = skill_delta(&skills_before, &app.cached_skills); |
| 40 | if !delta.is_empty() { |
| 41 | message.push_str(&format!(" ({delta})")); |
| 42 | } |
| 43 | message.push('.'); |
| 44 | if codewhale_plugins::provider_declarations_changed( |
| 45 | before.as_ref(), |
| 46 | app.plugin_registry.as_ref(), |
| 47 | ) { |
| 48 | message.push(' '); |
| 49 | message.push_str(codewhale_plugins::PLUGIN_PROVIDER_NEW_SESSION_NOTICE); |
| 50 | } |
| 51 | let result = commands::CommandResult::with_message_and_action( |
| 52 | message, |
| 53 | AppAction::PluginRegistryChanged, |
| 54 | ); |
| 55 | apply_command_result(terminal, app, engine_handle, config, result).await?; |
| 56 | } |
| 57 | if change.mcp { |
| 58 | let extensions_was_open = app.view_stack.extensions_is_top(); |
| 59 | handle_mcp_ui_action( |
| 60 | app, |
| 61 | engine_handle, |
| 62 | config, |
| 63 | crate::tui::app::McpUiAction::Reload, |
| 64 | ) |
| 65 | .await; |
| 66 | if !extensions_was_open && app.view_stack.extensions_is_top() { |
| 67 | app.view_stack.pop(); |
| 68 | } |
| 69 | } |
| 70 | app.needs_redraw = true; |
| 71 | Ok(()) |
| 72 | } |
| 73 |