trustformers-wasm 0.2.0

WebAssembly bindings for TrustformeRS transformer library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// Plugin Framework for TrustformeRS WASM
// Enables community extensions and custom functionality

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use wasm_bindgen::prelude::*;

/// Plugin trait that all plugins must implement
pub trait Plugin: Send + Sync {
    /// Plugin metadata
    fn metadata(&self) -> PluginMetadata;

    /// Initialize the plugin
    fn initialize(&mut self, config: PluginConfig) -> Result<(), PluginError>;

    /// Execute plugin functionality
    fn execute(&self, context: &PluginContext) -> Result<PluginResult, PluginError>;

    /// Cleanup resources when plugin is unloaded
    fn cleanup(&mut self);
}

/// Plugin metadata information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginMetadata {
    pub name: String,
    pub version: String,
    pub author: String,
    pub description: String,
    pub plugin_type: PluginType,
    pub dependencies: Vec<String>,
    pub permissions: Vec<PluginPermission>,
}

/// Types of plugins supported
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PluginType {
    /// Preprocessing plugins (tokenization, data processing)
    Preprocessor,
    /// Model inference plugins (custom model formats)
    InferenceEngine,
    /// Postprocessing plugins (output formatting, analysis)
    Postprocessor,
    /// Optimization plugins (quantization, pruning)
    Optimizer,
    /// Visualization plugins (charts, graphs)
    Visualizer,
    /// Storage plugins (custom backends)
    Storage,
    /// Network plugins (custom protocols)
    Network,
    /// Utility plugins (general purpose)
    Utility,
}

/// Plugin permissions system
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PluginPermission {
    /// Access to read model data
    ReadModelData,
    /// Access to modify model data
    WriteModelData,
    /// Access to network operations
    NetworkAccess,
    /// Access to local storage
    StorageAccess,
    /// Access to GPU resources
    GpuAccess,
    /// Access to performance profiling
    ProfilingAccess,
    /// Access to debug information
    DebugAccess,
    /// Access to user interface
    UiAccess,
}

/// Plugin configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginConfig {
    pub settings: HashMap<String, String>,
    pub enabled_features: Vec<String>,
    pub resource_limits: ResourceLimits,
}

/// Resource limits for plugins
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceLimits {
    pub max_memory_mb: Option<usize>,
    pub max_execution_time_ms: Option<u64>,
    pub max_network_requests: Option<usize>,
    pub max_gpu_memory_mb: Option<usize>,
}

/// Plugin execution context
#[derive(Debug)]
pub struct PluginContext {
    pub plugin_id: String,
    pub session_id: String,
    pub request_data: HashMap<String, String>,
    pub model_metadata: Option<ModelMetadata>,
    pub performance_budget: PerformanceBudget,
}

/// Serializable version of PluginContext for JavaScript interop
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializablePluginContext {
    pub plugin_id: String,
    pub session_id: String,
    pub request_data: HashMap<String, String>,
    pub model_metadata: Option<SerializableModelMetadata>,
    pub performance_budget: SerializablePerformanceBudget,
}

/// Model metadata for plugin context
#[derive(Debug, Clone)]
pub struct ModelMetadata {
    pub model_type: String,
    pub size_mb: f64,
    pub architecture: String,
    pub precision: String,
}

/// Serializable version of ModelMetadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializableModelMetadata {
    pub model_type: String,
    pub size_mb: f64,
    pub architecture: String,
    pub precision: String,
}

/// Performance budget for plugin execution
#[derive(Debug, Clone)]
pub struct PerformanceBudget {
    pub max_latency_ms: u64,
    pub max_memory_mb: usize,
    pub priority: ExecutionPriority,
}

/// Serializable version of PerformanceBudget
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializablePerformanceBudget {
    pub max_latency_ms: u64,
    pub max_memory_mb: usize,
    pub priority: SerializableExecutionPriority,
}

/// Execution priority levels
#[derive(Debug, Clone, PartialEq)]
pub enum ExecutionPriority {
    Critical,
    High,
    Normal,
    Low,
    Background,
}

/// Serializable version of ExecutionPriority
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum SerializableExecutionPriority {
    Critical,
    High,
    Normal,
    Low,
    Background,
}

// Conversion implementations
impl From<ExecutionPriority> for SerializableExecutionPriority {
    fn from(priority: ExecutionPriority) -> Self {
        match priority {
            ExecutionPriority::Critical => SerializableExecutionPriority::Critical,
            ExecutionPriority::High => SerializableExecutionPriority::High,
            ExecutionPriority::Normal => SerializableExecutionPriority::Normal,
            ExecutionPriority::Low => SerializableExecutionPriority::Low,
            ExecutionPriority::Background => SerializableExecutionPriority::Background,
        }
    }
}

impl From<ModelMetadata> for SerializableModelMetadata {
    fn from(metadata: ModelMetadata) -> Self {
        SerializableModelMetadata {
            model_type: metadata.model_type,
            size_mb: metadata.size_mb,
            architecture: metadata.architecture,
            precision: metadata.precision,
        }
    }
}

impl From<PerformanceBudget> for SerializablePerformanceBudget {
    fn from(budget: PerformanceBudget) -> Self {
        SerializablePerformanceBudget {
            max_latency_ms: budget.max_latency_ms,
            max_memory_mb: budget.max_memory_mb,
            priority: budget.priority.into(),
        }
    }
}

impl From<PluginContext> for SerializablePluginContext {
    fn from(context: PluginContext) -> Self {
        SerializablePluginContext {
            plugin_id: context.plugin_id,
            session_id: context.session_id,
            request_data: context.request_data,
            model_metadata: context.model_metadata.map(|m| m.into()),
            performance_budget: context.performance_budget.into(),
        }
    }
}

/// Plugin execution result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginResult {
    pub success: bool,
    pub data: HashMap<String, String>,
    pub metrics: ExecutionMetrics,
    pub messages: Vec<String>,
}

/// Execution metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionMetrics {
    pub execution_time_ms: f64,
    pub memory_used_mb: f64,
    pub cpu_usage_percent: f64,
    pub gpu_memory_used_mb: Option<f64>,
}

/// Plugin errors
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginError {
    pub code: PluginErrorCode,
    pub message: String,
    pub details: Option<String>,
}

/// Plugin error codes
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PluginErrorCode {
    InitializationFailed,
    ExecutionFailed,
    PermissionDenied,
    ResourceExhausted,
    InvalidConfiguration,
    DependencyMissing,
    UnsupportedOperation,
    Internal,
}

/// Plugin registry for managing loaded plugins
pub struct PluginRegistry {
    plugins: Arc<Mutex<HashMap<String, Box<dyn Plugin>>>>,
    enabled_plugins: Arc<Mutex<Vec<String>>>,
    plugin_configs: Arc<Mutex<HashMap<String, PluginConfig>>>,
}

impl Default for PluginRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl PluginRegistry {
    /// Create a new plugin registry
    pub fn new() -> Self {
        Self {
            plugins: Arc::new(Mutex::new(HashMap::new())),
            enabled_plugins: Arc::new(Mutex::new(Vec::new())),
            plugin_configs: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Register a new plugin
    pub fn register_plugin(
        &self,
        plugin_id: String,
        plugin: Box<dyn Plugin>,
        config: PluginConfig,
    ) -> Result<(), PluginError> {
        // Validate plugin metadata
        let metadata = plugin.metadata();
        self.validate_plugin_metadata(&metadata)?;

        // Check dependencies
        self.check_plugin_dependencies(&metadata.dependencies)?;

        // Store plugin and configuration
        {
            let mut plugins = self.plugins.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
            plugins.insert(plugin_id.clone(), plugin);
        }

        {
            let mut configs =
                self.plugin_configs.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
            configs.insert(plugin_id, config);
        }

        Ok(())
    }

    /// Enable a plugin
    pub fn enable_plugin(&self, plugin_id: &str) -> Result<(), PluginError> {
        // Check if plugin exists
        {
            let plugins = self.plugins.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
            if !plugins.contains_key(plugin_id) {
                return Err(PluginError {
                    code: PluginErrorCode::DependencyMissing,
                    message: format!("Plugin '{plugin_id}' not found"),
                    details: None,
                });
            }
        }

        // Initialize plugin
        {
            let mut plugins = self.plugins.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
            let configs =
                self.plugin_configs.lock().unwrap_or_else(|poisoned| poisoned.into_inner());

            if let (Some(plugin), Some(config)) =
                (plugins.get_mut(plugin_id), configs.get(plugin_id))
            {
                plugin.initialize(config.clone())?;
            }
        }

        // Add to enabled list
        {
            let mut enabled =
                self.enabled_plugins.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
            if !enabled.contains(&plugin_id.to_string()) {
                enabled.push(plugin_id.to_string());
            }
        }

        Ok(())
    }

    /// Disable a plugin
    pub fn disable_plugin(&self, plugin_id: &str) -> Result<(), PluginError> {
        // Remove from enabled list
        {
            let mut enabled =
                self.enabled_plugins.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
            enabled.retain(|id| id != plugin_id);
        }

        // Cleanup plugin
        {
            let mut plugins = self.plugins.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
            if let Some(plugin) = plugins.get_mut(plugin_id) {
                plugin.cleanup();
            }
        }

        Ok(())
    }

    /// Execute plugins of a specific type
    pub fn execute_plugins(
        &self,
        plugin_type: PluginType,
        context: &PluginContext,
    ) -> Vec<Result<PluginResult, PluginError>> {
        let enabled = self
            .enabled_plugins
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .clone();
        let plugins = self.plugins.lock().unwrap_or_else(|poisoned| poisoned.into_inner());

        let mut results = Vec::new();

        for plugin_id in enabled {
            if let Some(plugin) = plugins.get(&plugin_id) {
                let metadata = plugin.metadata();
                if metadata.plugin_type == plugin_type {
                    let result = plugin.execute(context);
                    results.push(result);
                }
            }
        }

        results
    }

    /// Get enabled plugins
    pub fn get_enabled_plugins(&self) -> Vec<String> {
        self.enabled_plugins
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .clone()
    }

    /// Get plugin metadata
    pub fn get_plugin_metadata(&self, plugin_id: &str) -> Option<PluginMetadata> {
        let plugins = self.plugins.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
        plugins.get(plugin_id).map(|plugin| plugin.metadata())
    }

    /// List all registered plugins
    pub fn list_plugins(&self) -> Vec<PluginMetadata> {
        let plugins = self.plugins.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
        plugins.values().map(|plugin| plugin.metadata()).collect()
    }

    fn validate_plugin_metadata(&self, metadata: &PluginMetadata) -> Result<(), PluginError> {
        if metadata.name.is_empty() {
            return Err(PluginError {
                code: PluginErrorCode::InvalidConfiguration,
                message: "Plugin name cannot be empty".to_string(),
                details: None,
            });
        }

        if metadata.version.is_empty() {
            return Err(PluginError {
                code: PluginErrorCode::InvalidConfiguration,
                message: "Plugin version cannot be empty".to_string(),
                details: None,
            });
        }

        Ok(())
    }

    fn check_plugin_dependencies(&self, dependencies: &[String]) -> Result<(), PluginError> {
        let plugins = self.plugins.lock().unwrap_or_else(|poisoned| poisoned.into_inner());

        for dep in dependencies {
            if !plugins.contains_key(dep) {
                return Err(PluginError {
                    code: PluginErrorCode::DependencyMissing,
                    message: format!("Missing dependency: {dep}"),
                    details: None,
                });
            }
        }

        Ok(())
    }
}

/// WASM bindings for plugin framework
#[wasm_bindgen]
pub struct PluginManager {
    registry: PluginRegistry,
}

impl Default for PluginManager {
    fn default() -> Self {
        Self::new()
    }
}

#[wasm_bindgen]
impl PluginManager {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self {
            registry: PluginRegistry::new(),
        }
    }

    /// Get list of enabled plugins as JSON
    #[wasm_bindgen(getter)]
    pub fn enabled_plugins(&self) -> String {
        serde_json::to_string(&self.registry.get_enabled_plugins()).unwrap_or_default()
    }

    /// Get list of all plugins as JSON
    pub fn list_plugins(&self) -> String {
        serde_json::to_string(&self.registry.list_plugins()).unwrap_or_default()
    }

    /// Enable a plugin by ID
    pub fn enable_plugin(&self, plugin_id: &str) -> Result<(), JsValue> {
        self.registry
            .enable_plugin(plugin_id)
            .map_err(|e| JsValue::from_str(&e.message))
    }

    /// Disable a plugin by ID
    pub fn disable_plugin(&self, plugin_id: &str) -> Result<(), JsValue> {
        self.registry
            .disable_plugin(plugin_id)
            .map_err(|e| JsValue::from_str(&e.message))
    }

    /// Get plugin metadata as JSON
    pub fn get_plugin_metadata(&self, plugin_id: &str) -> Option<String> {
        self.registry
            .get_plugin_metadata(plugin_id)
            .and_then(|metadata| serde_json::to_string(&metadata).ok())
    }
}

/// Default resource limits for plugins
impl Default for ResourceLimits {
    fn default() -> Self {
        Self {
            max_memory_mb: Some(100),
            max_execution_time_ms: Some(5000),
            max_network_requests: Some(10),
            max_gpu_memory_mb: Some(50),
        }
    }
}

/// Default plugin configuration
impl Default for PluginConfig {
    fn default() -> Self {
        Self {
            settings: HashMap::new(),
            enabled_features: Vec::new(),
            resource_limits: ResourceLimits::default(),
        }
    }
}

/// Utility functions for creating plugin configs
#[wasm_bindgen]
pub fn create_default_plugin_config() -> String {
    serde_json::to_string(&PluginConfig::default()).unwrap_or_default()
}

#[wasm_bindgen]
pub fn create_plugin_context(plugin_id: &str, session_id: &str, request_data: &str) -> String {
    let request_map: HashMap<String, String> =
        serde_json::from_str(request_data).unwrap_or_default();

    let context = PluginContext {
        plugin_id: plugin_id.to_string(),
        session_id: session_id.to_string(),
        request_data: request_map,
        model_metadata: Some(ModelMetadata {
            model_type: "transformer".to_string(),
            size_mb: 125.5,
            architecture: "gpt-2".to_string(),
            precision: "fp16".to_string(),
        }),
        performance_budget: PerformanceBudget {
            max_latency_ms: 1000,
            max_memory_mb: 50,
            priority: ExecutionPriority::Normal,
        },
    };

    // Convert to serializable version and serialize to JSON
    let serializable_context: SerializablePluginContext = context.into();
    serde_json::to_string(&serializable_context).unwrap_or_else(|e| {
        web_sys::console::log_1(&format!("Failed to serialize plugin context: {}", e).into());
        format!(
            r#"{{"plugin_id":"{}","session_id":"{}","error":"serialization_failed"}}"#,
            plugin_id, session_id
        )
    })
}