openclaw_plugins/
registry.rs1use std::collections::HashMap;
4use std::sync::Arc;
5
6use crate::api::{Plugin, PluginError, PluginHook};
7
8pub struct PluginRegistry {
10 plugins: HashMap<String, Arc<dyn Plugin>>,
11}
12
13impl PluginRegistry {
14 #[must_use]
16 pub fn new() -> Self {
17 Self {
18 plugins: HashMap::new(),
19 }
20 }
21
22 pub fn register(&mut self, plugin: Arc<dyn Plugin>) {
24 self.plugins.insert(plugin.id().to_string(), plugin);
25 }
26
27 #[must_use]
29 pub fn get(&self, id: &str) -> Option<&Arc<dyn Plugin>> {
30 self.plugins.get(id)
31 }
32
33 #[must_use]
35 pub fn list(&self) -> Vec<&str> {
36 self.plugins.keys().map(String::as_str).collect()
37 }
38
39 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}