Skip to main content

llm_test_bench_core/plugins/
mod.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7//! # WASM-based Plugin System
8//!
9//! This module provides an enterprise-grade, secure plugin system using WebAssembly.
10//!
11//! ## Architecture
12//!
13//! ```text
14//! ┌─────────────────────────────────────────────────────────────┐
15//! │                    LLM Test Bench Host                      │
16//! │                                                             │
17//! │  ┌───────────────────────────────────────────────────────┐ │
18//! │  │              Plugin Manager                           │ │
19//! │  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  │ │
20//! │  │  │   Plugin    │  │   Plugin    │  │   Plugin    │  │ │
21//! │  │  │  Registry   │  │   Loader    │  │   Sandbox   │  │ │
22//! │  │  └─────────────┘  └─────────────┘  └─────────────┘  │ │
23//! │  └───────────────────────────────────────────────────────┘ │
24//! │                            │                                │
25//! │                            ▼                                │
26//! │  ┌───────────────────────────────────────────────────────┐ │
27//! │  │              WASM Runtime (wasmtime)                  │ │
28//! │  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  │ │
29//! │  │  │   Plugin A  │  │   Plugin B  │  │   Plugin C  │  │ │
30//! │  │  │  (sandboxed)│  │  (sandboxed)│  │  (sandboxed)│  │ │
31//! │  │  └─────────────┘  └─────────────┘  └─────────────┘  │ │
32//! │  └───────────────────────────────────────────────────────┘ │
33//! └─────────────────────────────────────────────────────────────┘
34//! ```
35//!
36//! ## Features
37//!
38//! - **Security**: Sandboxed execution with resource limits
39//! - **Performance**: Native-speed execution with WASM
40//! - **Portability**: Write once, run anywhere
41//! - **Type Safety**: Strongly-typed plugin API
42//! - **Hot Reloading**: Load/unload plugins at runtime
43//!
44//! ## Plugin Types
45//!
46//! ### Evaluator Plugins
47//! Custom evaluation metrics for LLM outputs.
48//!
49//! ### Provider Plugins
50//! Custom LLM provider integrations.
51//!
52//! ### Transform Plugins
53//! Data transformation and preprocessing.
54//!
55//! ### Filter Plugins
56//! Result filtering and post-processing.
57//!
58//! ## Usage
59//!
60//! ```rust,no_run
61//! use llm_test_bench_core::plugins::{PluginManager, PluginConfig};
62//!
63//! #[tokio::main]
64//! async fn main() -> anyhow::Result<()> {
65//!     // Initialize plugin manager
66//!     let mut manager = PluginManager::new()?;
67//!
68//!     // Load a plugin
69//!     let plugin_id = manager.load_plugin("path/to/plugin.wasm").await?;
70//!
71//!     // Execute plugin
72//!     let result = manager.execute_plugin(&plugin_id, input).await?;
73//!
74//!     // Unload plugin
75//!     manager.unload_plugin(&plugin_id).await?;
76//!
77//!     Ok(())
78//! }
79//! ```
80//!
81//! ## Security
82//!
83//! All plugins run in a sandboxed WebAssembly environment with:
84//! - Memory limits
85//! - CPU time limits
86//! - No direct filesystem access (WASI controlled)
87//! - No network access by default
88//! - Capability-based security
89
90pub mod types;
91pub mod api;
92pub mod runtime;
93pub mod manager;
94pub mod loader;
95pub mod sandbox;
96pub mod registry;
97pub mod host;
98
99pub use types::{
100    PluginType, PluginMetadata, PluginConfig, PluginPermissions,
101    PluginInfo, PluginStatus, PluginCapability,
102};
103
104pub use api::{
105    PluginApi, EvaluatorPlugin, ProviderPlugin, TransformPlugin, FilterPlugin,
106    PluginInput, PluginOutput, PluginResult,
107};
108
109pub use runtime::{WasmRuntime, RuntimeConfig, RuntimeLimits};
110pub use manager::{PluginManager, ManagerConfig};
111pub use loader::{PluginLoader, LoaderConfig};
112pub use sandbox::{PluginSandbox, SandboxConfig};
113pub use registry::{PluginRegistry, RegistryConfig};
114pub use host::{HostFunctions, HostContext};
115
116use anyhow::Result;
117use std::sync::Arc;
118use parking_lot::RwLock;
119
120/// Main plugin system coordinator
121pub struct PluginSystem {
122    manager: Arc<PluginManager>,
123    registry: Arc<RwLock<PluginRegistry>>,
124}
125
126impl PluginSystem {
127    /// Create a new plugin system
128    pub fn new() -> Result<Self> {
129        let manager = Arc::new(PluginManager::new(ManagerConfig::default())?);
130        let registry = Arc::new(RwLock::new(PluginRegistry::new(RegistryConfig::default())));
131
132        Ok(Self { manager, registry })
133    }
134
135    /// Create a plugin system with custom configuration
136    pub fn with_config(manager_config: ManagerConfig, registry_config: RegistryConfig) -> Result<Self> {
137        let manager = Arc::new(PluginManager::new(manager_config)?);
138        let registry = Arc::new(RwLock::new(PluginRegistry::new(registry_config)));
139
140        Ok(Self { manager, registry })
141    }
142
143    /// Load a plugin from a file
144    pub async fn load_plugin(&self, path: impl AsRef<std::path::Path>) -> Result<String> {
145        let plugin_id = self.manager.load_plugin(path).await?;
146
147        // Get plugin info and register it
148        if let Some(info) = self.manager.get_plugin_info(&plugin_id).await {
149            let mut registry = self.registry.write();
150            registry.register_plugin(info)?;
151        }
152
153        Ok(plugin_id)
154    }
155
156    /// Load a plugin from bytes
157    pub async fn load_plugin_from_bytes(&self, name: String, bytes: Vec<u8>) -> Result<String> {
158        let plugin_id = self.manager.load_plugin_from_bytes(name, bytes).await?;
159
160        // Get plugin info and register it
161        if let Some(info) = self.manager.get_plugin_info(&plugin_id).await {
162            let mut registry = self.registry.write();
163            registry.register_plugin(info)?;
164        }
165
166        Ok(plugin_id)
167    }
168
169    /// Unload a plugin
170    pub async fn unload_plugin(&self, plugin_id: &str) -> Result<()> {
171        self.manager.unload_plugin(plugin_id).await?;
172
173        let mut registry = self.registry.write();
174        registry.unregister_plugin(plugin_id);
175
176        Ok(())
177    }
178
179    /// Execute a plugin
180    pub async fn execute_plugin(
181        &self,
182        plugin_id: &str,
183        input: PluginInput,
184    ) -> Result<PluginOutput> {
185        self.manager.execute_plugin(plugin_id, input).await
186    }
187
188    /// List all loaded plugins
189    pub async fn list_plugins(&self) -> Vec<PluginInfo> {
190        self.manager.list_plugins().await
191    }
192
193    /// Get plugin info
194    pub async fn get_plugin_info(&self, plugin_id: &str) -> Option<PluginInfo> {
195        self.manager.get_plugin_info(plugin_id).await
196    }
197
198    /// Find plugins by type
199    pub fn find_plugins_by_type(&self, plugin_type: PluginType) -> Vec<PluginInfo> {
200        self.registry.read().find_by_type(plugin_type)
201    }
202
203    /// Find plugins by capability
204    pub fn find_plugins_by_capability(&self, capability: PluginCapability) -> Vec<PluginInfo> {
205        self.registry.read().find_by_capability(capability)
206    }
207
208    /// Get the plugin manager
209    pub fn manager(&self) -> Arc<PluginManager> {
210        self.manager.clone()
211    }
212
213    /// Get the plugin registry
214    pub fn registry(&self) -> Arc<RwLock<PluginRegistry>> {
215        self.registry.clone()
216    }
217}
218
219impl Default for PluginSystem {
220    fn default() -> Self {
221        Self::new().expect("Failed to create default plugin system")
222    }
223}
224
225#[cfg(test)]
226mod tests {
227    use super::*;
228
229    #[test]
230    fn test_plugin_system_creation() {
231        let system = PluginSystem::new();
232        assert!(system.is_ok());
233    }
234
235    #[tokio::test]
236    async fn test_plugin_system_list_empty() {
237        let system = PluginSystem::new().unwrap();
238        let plugins = system.list_plugins().await;
239        assert_eq!(plugins.len(), 0);
240    }
241}