pub struct PluginManager { /* private fields */ }Expand description
Plugin manager
Manages the lifecycle of all plugins in the system. This includes:
- Registering new plugins
- Initializing plugins on startup
- Enabling/disabling plugins at runtime
- Executing plugin hooks
- Shutting down plugins gracefully
§Thread Safety
The PluginManager is not thread-safe by itself. It should be wrapped
in Arc<RwLock<PluginManager>> for concurrent access.
§Example
use llm_memory_graph::plugin::PluginManager;
use std::sync::Arc;
use tokio::sync::RwLock;
let mut manager = PluginManager::new();
// Register plugins...
manager.init_all().await?;
// Wrap for concurrent access
let manager = Arc::new(RwLock::new(manager));Implementations§
Source§impl PluginManager
impl PluginManager
Sourcepub fn with_api_version(api_version: impl Into<String>) -> Self
pub fn with_api_version(api_version: impl Into<String>) -> Self
Create a plugin manager with a specific API version
Sourcepub fn register(&mut self, plugin: Arc<dyn Plugin>) -> Result<(), PluginError>
pub fn register(&mut self, plugin: Arc<dyn Plugin>) -> Result<(), PluginError>
Register a plugin
Registers a new plugin with the manager. The plugin must not already
be registered. After registration, the plugin is in the Registered state
and must be initialized before it can be used.
§Errors
Returns an error if:
- The plugin is already registered
- The plugin’s API version is incompatible
Sourcepub fn unregister(&mut self, name: &str) -> Result<(), PluginError>
pub fn unregister(&mut self, name: &str) -> Result<(), PluginError>
Unregister a plugin
Removes a plugin from the manager. The plugin must be disabled before it can be unregistered.
§Errors
Returns an error if:
- The plugin is not found
- The plugin is still enabled
Sourcepub async fn initialize(&mut self, name: &str) -> Result<(), PluginError>
pub async fn initialize(&mut self, name: &str) -> Result<(), PluginError>
Initialize a specific plugin
Initializes a registered plugin, calling its init() method.
After successful initialization, the plugin is in the Initialized state.
Sourcepub fn enable(&mut self, name: &str) -> Result<(), PluginError>
pub fn enable(&mut self, name: &str) -> Result<(), PluginError>
Enable a plugin
Enables an initialized plugin, making it active and ready to execute hooks.
§Errors
Returns an error if:
- The plugin is not found
- The plugin is not initialized
Sourcepub fn disable(&mut self, name: &str) -> Result<(), PluginError>
pub fn disable(&mut self, name: &str) -> Result<(), PluginError>
Disable a plugin
Disables an enabled plugin, preventing it from executing hooks.
§Errors
Returns an error if the plugin is not found
Sourcepub fn active_plugins(&self) -> Vec<Arc<dyn Plugin>>
pub fn active_plugins(&self) -> Vec<Arc<dyn Plugin>>
Get active plugins
Returns a list of all enabled plugins that are ready to execute.
Sourcepub fn all_plugins(&self) -> Vec<(PluginMetadata, PluginState)>
pub fn all_plugins(&self) -> Vec<(PluginMetadata, PluginState)>
Get all plugins regardless of state
Sourcepub fn get_state(&self, name: &str) -> Option<PluginState>
pub fn get_state(&self, name: &str) -> Option<PluginState>
Get plugin state
Sourcepub fn is_enabled(&self, name: &str) -> bool
pub fn is_enabled(&self, name: &str) -> bool
Check if a plugin is enabled
Sourcepub async fn init_all(&mut self) -> Result<(), PluginError>
pub async fn init_all(&mut self) -> Result<(), PluginError>
Initialize all registered plugins
Initializes all plugins that are in the Registered state.
Continues even if some plugins fail to initialize.
§Errors
Returns an error if any plugin fails to initialize. The error contains information about the first failure encountered.
Sourcepub fn enable_all(&mut self) -> Result<(), PluginError>
pub fn enable_all(&mut self) -> Result<(), PluginError>
Enable all initialized plugins
Sourcepub fn disable_all(&mut self) -> Result<(), PluginError>
pub fn disable_all(&mut self) -> Result<(), PluginError>
Disable all plugins
Sourcepub async fn shutdown_all(&mut self) -> Result<(), PluginError>
pub async fn shutdown_all(&mut self) -> Result<(), PluginError>
Shutdown all plugins
Calls shutdown() on all plugins and removes them from the manager.
Sourcepub async fn execute_before_hooks(
&self,
hook_name: &str,
context: &PluginContext,
) -> Result<(), PluginError>
pub async fn execute_before_hooks( &self, hook_name: &str, context: &PluginContext, ) -> Result<(), PluginError>
Execute before hooks for all active plugins
Executes the specified hook on all enabled plugins in registration order. If any plugin returns an error, execution stops and the error is returned.
Sourcepub async fn execute_after_hooks(
&self,
hook_name: &str,
context: &PluginContext,
) -> Result<(), PluginError>
pub async fn execute_after_hooks( &self, hook_name: &str, context: &PluginContext, ) -> Result<(), PluginError>
Execute after hooks for all active plugins
Executes the specified hook on all enabled plugins in registration order. Unlike before hooks, errors in after hooks are logged but don’t stop execution.
Sourcepub fn load_from_directory(
&mut self,
_path: impl AsRef<Path>,
) -> Result<(), PluginError>
pub fn load_from_directory( &mut self, _path: impl AsRef<Path>, ) -> Result<(), PluginError>
Load plugins from directory (dynamic loading - future)
This is a placeholder for future dynamic plugin loading functionality. Currently, plugins must be compiled into the application.
Sourcepub fn list_plugins(&self) -> Vec<(PluginMetadata, PluginState)>
pub fn list_plugins(&self) -> Vec<(PluginMetadata, PluginState)>
List all plugins with their metadata and state
Sourcepub fn count_by_state(&self) -> HashMap<PluginState, usize>
pub fn count_by_state(&self) -> HashMap<PluginState, usize>
Get plugin count by state