Skip to main content

openclaw_plugins/
registry.rs

1//! Plugin registry.
2
3use std::collections::HashMap;
4use std::sync::Arc;
5
6use crate::api::{Plugin, PluginError, PluginHook};
7
8/// Registry of loaded plugins.
9pub struct PluginRegistry {
10    plugins: HashMap<String, Arc<dyn Plugin>>,
11}
12
13impl PluginRegistry {
14    /// Create a new empty registry.
15    #[must_use]
16    pub fn new() -> Self {
17        Self {
18            plugins: HashMap::new(),
19        }
20    }
21
22    /// Register a plugin.
23    pub fn register(&mut self, plugin: Arc<dyn Plugin>) {
24        self.plugins.insert(plugin.id().to_string(), plugin);
25    }
26
27    /// Get a plugin by ID.
28    #[must_use]
29    pub fn get(&self, id: &str) -> Option<&Arc<dyn Plugin>> {
30        self.plugins.get(id)
31    }
32
33    /// List all plugin IDs.
34    #[must_use]
35    pub fn list(&self) -> Vec<&str> {
36        self.plugins.keys().map(String::as_str).collect()
37    }
38
39    /// Execute a hook on all plugins that support it.
40    pub async fn execute_hook(
41        &self,
42        hook: PluginHook,
43        data: serde_json::Value,
44    ) -> Vec<Result<serde_json::Value, PluginError>> {
45        let mut results = Vec::new();
46
47        for plugin in self.plugins.values() {
48            if plugin.hooks().contains(&hook) {
49                results.push(plugin.execute_hook(hook, data.clone()).await);
50            }
51        }
52
53        results
54    }
55}
56
57impl Default for PluginRegistry {
58    fn default() -> Self {
59        Self::new()
60    }
61}