aiway_plugin/
manager.rs

1use crate::protocol::gateway::HttpContext;
2use crate::{Plugin, PluginError};
3use dashmap::DashMap;
4use serde_json::Value;
5
6pub struct PluginManager {
7    plugins: DashMap<String, Box<dyn Plugin>>,
8}
9impl PluginManager {
10    pub fn new() -> Self {
11        Self {
12            plugins: Default::default(),
13        }
14    }
15
16    pub fn register(&self, plugin: Box<dyn Plugin>) {
17        self.plugins.insert(plugin.name().to_string(), plugin);
18    }
19
20    pub async fn run(
21        &self,
22        name: &str,
23        context: &HttpContext,
24        config: &Value,
25    ) -> Result<Value, PluginError> {
26        if let Some(plugin) = self.plugins.get(name) {
27            plugin.execute(context, config).await
28        } else {
29            Err(PluginError::NotFound(name.to_string()))
30        }
31    }
32
33    pub fn clear(&self) {
34        self.plugins.clear();
35    }
36}
37
38impl Default for PluginManager {
39    fn default() -> Self {
40        Self::new()
41    }
42}