//! Plugin mention capability enrichment for the TUI. //! //! Mention inventory comes from app-server `plugin/installed` so loading //! mentions does not fetch the browsable plugin catalog. The API exposes //! plugin-level mention metadata, but not effective per-session capability summaries. use super::*; use akode_app_server_protocol::PluginInstalledParams; use akode_app_server_protocol::PluginInstalledResponse; use akode_app_server_protocol::PluginSummary; use akode_app_server_protocol::RequestId; use akode_plugin::PluginCapabilitySummary; pub(super) async fn fetch_plugin_mentions( request_handle: AppServerRequestHandle, cwd: PathBuf, ) -> Result> { let cwd = AbsolutePathBuf::try_from(cwd).wrap_err("plugin installed cwd must be absolute")?; let request_id = RequestId::String(format!("plugin-installed-{}", Uuid::new_v4())); let response = request_handle .request_typed(ClientRequest::PluginInstalled { request_id, params: PluginInstalledParams { cwds: Some(vec![cwd]), install_suggestion_plugin_names: None, }, }) .await .wrap_err("plugin/installed failed in TUI")?; Ok(plugin_mentions_from_installed_response(response)) } fn plugin_mentions_from_installed_response( response: PluginInstalledResponse, ) -> Vec { response .marketplaces .into_iter() .flat_map(|marketplace| { let marketplace_name = marketplace.name; marketplace .plugins .into_iter() .filter_map(move |plugin| plugin_mention_from_summary(&marketplace_name, plugin)) }) .collect() } fn plugin_is_eligible_for_mentions(plugin: &PluginSummary) -> bool { plugin.installed && plugin.enabled } fn plugin_mention_from_summary( marketplace_name: &str, plugin: PluginSummary, ) -> Option { if !plugin_is_eligible_for_mentions(&plugin) { return None; } Some(PluginCapabilitySummary { config_name: plugin.id.clone(), display_name: plugin_mention_display_name(&plugin), plugin_namespace: Some(plugin.name.clone()), description: plugin_mention_description(marketplace_name, &plugin), has_skills: false, mcp_server_names: Vec::new(), }) } fn plugin_mention_display_name(plugin: &PluginSummary) -> String { plugin .interface .as_ref() .and_then(|interface| interface.display_name.as_deref()) .map(str::trim) .filter(|display_name| !display_name.is_empty()) .map(str::to_string) .unwrap_or_else(|| plugin.name.clone()) } fn plugin_mention_description(marketplace_name: &str, plugin: &PluginSummary) -> Option { plugin .interface .as_ref() .and_then(|interface| interface.short_description.as_deref()) .map(str::trim) .filter(|description| !description.is_empty()) .map(str::to_string) .or_else(|| { let marketplace_name = marketplace_name.trim(); (!marketplace_name.is_empty()).then(|| marketplace_name.to_string()) }) } #[cfg(test)] mod tests { use super::*; use akode_app_server_protocol::PluginAuthPolicy; use akode_app_server_protocol::PluginInstallPolicy; use akode_app_server_protocol::PluginMarketplaceEntry; use akode_app_server_protocol::PluginSource; use akode_utils_absolute_path::AbsolutePathBuf; use pretty_assertions::assert_eq; #[test] fn plugin_mentions_use_installed_summaries_and_gui_eligibility() { let active = plugin_summary("active"); let active_second = plugin_summary("active-second"); let mut disabled = plugin_summary("disabled"); disabled.enabled = false; let mut uninstalled = plugin_summary("uninstalled"); uninstalled.installed = false; let response = PluginInstalledResponse { marketplaces: vec![PluginMarketplaceEntry { name: "server-marketplace".to_string(), path: None, interface: None, plugins: vec![active, active_second, disabled, uninstalled], }], marketplace_load_errors: Vec::new(), }; assert_eq!( plugin_mentions_from_installed_response(response), ["active", "active-second"].map(|name| PluginCapabilitySummary { config_name: format!("{name}@server-marketplace"), display_name: name.to_string(), plugin_namespace: Some(name.to_string()), description: Some("server-marketplace".to_string()), has_skills: false, mcp_server_names: Vec::new(), }) ); } fn plugin_summary(name: &str) -> PluginSummary { PluginSummary { id: format!("{name}@server-marketplace"), local_version: None, name: name.to_string(), source: PluginSource::Local { path: AbsolutePathBuf::try_from(std::env::temp_dir().join(name)) .expect("absolute plugin path"), }, installed: true, enabled: true, install_policy: PluginInstallPolicy::Available, auth_policy: PluginAuthPolicy::OnInstall, interface: None, keywords: Vec::new(), } } }