universal-bot-core 1.0.0

Core functionality for Universal Bot AI automation framework with AWS Bedrock
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//! Plugin system for extending bot functionality
//!
//! This module provides a plugin architecture that allows extending
//! the bot's capabilities without modifying core code.

use std::collections::HashMap;
use std::fmt;

use anyhow::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use tracing::{debug, info, instrument, warn};

use crate::{
    error::Error,
    message::{Message, Response},
};

/// Plugin trait for extending bot functionality
#[async_trait]
pub trait Plugin: Send + Sync {
    /// Get the plugin name
    fn name(&self) -> &str;

    /// Get the plugin version
    fn version(&self) -> &str;

    /// Get plugin description
    fn description(&self) -> &str {
        "No description provided"
    }

    /// Get plugin capabilities
    fn capabilities(&self) -> Vec<Capability>;

    /// Initialize the plugin
    async fn initialize(&mut self, _config: PluginConfig) -> Result<()> {
        Ok(())
    }

    /// Process a request
    async fn process(&self, request: PluginRequest) -> Result<PluginResponse>;

    /// Shutdown the plugin
    async fn shutdown(&mut self) -> Result<()> {
        Ok(())
    }

    /// Check if the plugin can handle a message
    fn can_handle(&self, _message: &Message) -> bool {
        true
    }

    /// Get plugin metadata
    fn metadata(&self) -> PluginMetadata {
        PluginMetadata {
            name: self.name().to_string(),
            version: self.version().to_string(),
            description: self.description().to_string(),
            author: None,
            homepage: None,
            license: None,
        }
    }
}

/// Plugin capability
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Capability {
    /// Capability name
    pub name: String,
    /// Capability type
    pub capability_type: CapabilityType,
    /// Description
    pub description: String,
    /// Required permissions
    pub required_permissions: Vec<Permission>,
}

/// Type of capability
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CapabilityType {
    /// Message processing
    MessageProcessor,
    /// Command handler
    CommandHandler,
    /// Event listener
    EventListener,
    /// Tool provider
    ToolProvider,
    /// Middleware
    Middleware,
    /// Custom capability
    Custom(String),
}

/// Plugin permission
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Permission {
    /// Read messages
    ReadMessages,
    /// Write messages
    WriteMessages,
    /// Access context
    AccessContext,
    /// Modify context
    ModifyContext,
    /// Make network requests
    NetworkAccess,
    /// Access filesystem
    FileSystemAccess,
    /// Execute commands
    ExecuteCommands,
    /// Access database
    DatabaseAccess,
    /// All permissions
    All,
}

/// Plugin configuration
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PluginConfig {
    /// Plugin-specific settings
    pub settings: HashMap<String, serde_json::Value>,
    /// Enabled features
    pub enabled_features: Vec<String>,
    /// Granted permissions
    pub permissions: Vec<Permission>,
    /// Resource limits
    pub resource_limits: ResourceLimits,
}

/// Resource limits for plugins
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceLimits {
    /// Maximum memory in bytes
    pub max_memory: Option<usize>,
    /// Maximum CPU percentage
    pub max_cpu: Option<f32>,
    /// Maximum execution time
    pub max_execution_time: Option<std::time::Duration>,
    /// Maximum concurrent operations
    pub max_concurrent_ops: Option<usize>,
}

impl Default for ResourceLimits {
    fn default() -> Self {
        Self {
            max_memory: Some(100 * 1024 * 1024), // 100MB
            max_cpu: Some(50.0),                 // 50%
            max_execution_time: Some(std::time::Duration::from_secs(30)),
            max_concurrent_ops: Some(10),
        }
    }
}

/// Plugin request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginRequest {
    /// Request ID
    pub id: String,
    /// Request type
    pub request_type: RequestType,
    /// Request data
    pub data: serde_json::Value,
    /// Request metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Request type
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RequestType {
    /// Process a message
    ProcessMessage,
    /// Execute a command
    ExecuteCommand,
    /// Handle an event
    HandleEvent,
    /// Invoke a tool
    InvokeTool,
    /// Custom request
    Custom(String),
}

/// Plugin response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginResponse {
    /// Response ID
    pub id: String,
    /// Success status
    pub success: bool,
    /// Response data
    pub data: serde_json::Value,
    /// Error message if failed
    pub error: Option<String>,
    /// Response metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

impl PluginResponse {
    /// Create a successful response
    pub fn success(id: impl Into<String>, data: serde_json::Value) -> Self {
        Self {
            id: id.into(),
            success: true,
            data,
            error: None,
            metadata: HashMap::new(),
        }
    }

    /// Create an error response
    pub fn error(id: impl Into<String>, error: impl fmt::Display) -> Self {
        Self {
            id: id.into(),
            success: false,
            data: serde_json::Value::Null,
            error: Some(error.to_string()),
            metadata: HashMap::new(),
        }
    }
}

/// Plugin metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginMetadata {
    /// Plugin name
    pub name: String,
    /// Plugin version
    pub version: String,
    /// Plugin description
    pub description: String,
    /// Plugin author
    pub author: Option<String>,
    /// Plugin homepage
    pub homepage: Option<String>,
    /// Plugin license
    pub license: Option<String>,
}

/// Plugin registry for managing plugins
pub struct PluginRegistry {
    plugins: HashMap<String, Box<dyn Plugin>>,
    hooks: HashMap<HookType, Vec<String>>,
    permissions: HashMap<String, Vec<Permission>>,
}

impl PluginRegistry {
    /// Create a new plugin registry
    #[must_use]
    pub fn new() -> Self {
        Self {
            plugins: HashMap::new(),
            hooks: HashMap::new(),
            permissions: HashMap::new(),
        }
    }

    /// Register a plugin
    ///
    /// # Errors
    ///
    /// Returns an error if a plugin with the same name already exists.
    #[instrument(skip(self, plugin))]
    pub fn register(&mut self, mut plugin: Box<dyn Plugin>) -> Result<()> {
        let name = plugin.name().to_string();

        if self.plugins.contains_key(&name) {
            return Err(Error::Plugin(format!("Plugin '{name}' already registered")).into());
        }

        info!("Registering plugin: {} v{}", name, plugin.version());

        // Initialize plugin with default config
        let config = PluginConfig::default();
        futures::executor::block_on(plugin.initialize(config))?;

        // Register capabilities
        for capability in plugin.capabilities() {
            self.register_hook(&name, &capability);
        }

        self.plugins.insert(name.clone(), plugin);
        self.permissions.insert(name, vec![Permission::All]);

        Ok(())
    }

    /// Unregister a plugin
    #[instrument(skip(self))]
    pub async fn unregister(&mut self, name: &str) -> Result<()> {
        if let Some(mut plugin) = self.plugins.remove(name) {
            info!("Unregistering plugin: {}", name);
            plugin.shutdown().await?;

            // Remove from hooks
            for hooks in self.hooks.values_mut() {
                hooks.retain(|n| n != name);
            }

            self.permissions.remove(name);
            Ok(())
        } else {
            Err(Error::NotFound(format!("Plugin '{name}' not found")).into())
        }
    }

    /// Get a plugin by name
    pub fn get(&self, name: &str) -> Option<&dyn Plugin> {
        self.plugins.get(name).map(std::convert::AsRef::as_ref)
    }

    /// List all registered plugins
    pub fn list(&self) -> Vec<PluginMetadata> {
        self.plugins.values().map(|p| p.metadata()).collect()
    }

    /// Apply pre-processing plugins
    #[instrument(skip(self, message))]
    pub async fn apply_pre_processing(&self, mut message: Message) -> Result<Message> {
        for plugin in self.plugins.values() {
            if plugin.can_handle(&message) {
                let request = PluginRequest {
                    id: uuid::Uuid::new_v4().to_string(),
                    request_type: RequestType::ProcessMessage,
                    data: serde_json::to_value(&message)?,
                    metadata: HashMap::new(),
                };

                match plugin.process(request).await {
                    Ok(response) if response.success => {
                        if let Ok(processed) = serde_json::from_value(response.data) {
                            message = processed;
                        }
                    }
                    Ok(response) => {
                        warn!(
                            "Plugin {} failed to process message: {:?}",
                            plugin.name(),
                            response.error
                        );
                    }
                    Err(e) => {
                        warn!("Plugin {} error: {}", plugin.name(), e);
                    }
                }
            }
        }

        Ok(message)
    }

    /// Apply post-processing plugins
    #[instrument(skip(self, response))]
    pub async fn apply_post_processing(&self, mut response: Response) -> Result<Response> {
        for plugin in self.plugins.values() {
            let request = PluginRequest {
                id: uuid::Uuid::new_v4().to_string(),
                request_type: RequestType::Custom("post_process".to_string()),
                data: serde_json::to_value(&response)?,
                metadata: HashMap::new(),
            };

            match plugin.process(request).await {
                Ok(plugin_response) if plugin_response.success => {
                    if let Ok(processed) = serde_json::from_value(plugin_response.data) {
                        response = processed;
                    }
                }
                Ok(plugin_response) => {
                    debug!(
                        "Plugin {} post-processing failed: {:?}",
                        plugin.name(),
                        plugin_response.error
                    );
                }
                Err(e) => {
                    debug!("Plugin {} post-processing error: {}", plugin.name(), e);
                }
            }
        }

        Ok(response)
    }

    /// Check if a plugin has permission
    pub fn has_permission(&self, plugin_name: &str, permission: &Permission) -> bool {
        self.permissions
            .get(plugin_name)
            .is_some_and(|perms| perms.contains(permission) || perms.contains(&Permission::All))
    }

    // Private helper methods

    fn register_hook(&mut self, plugin_name: &str, capability: &Capability) {
        let hook_type = match &capability.capability_type {
            CapabilityType::MessageProcessor => HookType::MessageProcessor,
            CapabilityType::CommandHandler => HookType::CommandHandler,
            CapabilityType::EventListener => HookType::EventListener,
            CapabilityType::ToolProvider => HookType::ToolProvider,
            CapabilityType::Middleware => HookType::Middleware,
            CapabilityType::Custom(name) => HookType::Custom(name.clone()),
        };

        self.hooks
            .entry(hook_type)
            .or_default()
            .push(plugin_name.to_string());
    }
}

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

/// Hook type for plugin registration
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum HookType {
    /// Message processor hook
    MessageProcessor,
    /// Command handler hook
    CommandHandler,
    /// Event listener hook
    EventListener,
    /// Tool provider hook
    ToolProvider,
    /// Middleware hook
    Middleware,
    /// Custom hook
    Custom(String),
}

/// Example plugin implementation
pub struct EchoPlugin {
    name: String,
    version: String,
}

impl EchoPlugin {
    /// Create a new echo plugin
    #[must_use]
    pub fn new() -> Self {
        Self {
            name: "echo".to_string(),
            version: "1.0.0".to_string(),
        }
    }
}

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

#[async_trait]
impl Plugin for EchoPlugin {
    fn name(&self) -> &str {
        &self.name
    }

    fn version(&self) -> &str {
        &self.version
    }

    fn description(&self) -> &'static str {
        "Simple echo plugin for testing"
    }

    fn capabilities(&self) -> Vec<Capability> {
        vec![Capability {
            name: "echo".to_string(),
            capability_type: CapabilityType::MessageProcessor,
            description: "Echoes messages back".to_string(),
            required_permissions: vec![Permission::ReadMessages, Permission::WriteMessages],
        }]
    }

    async fn process(&self, request: PluginRequest) -> Result<PluginResponse> {
        match request.request_type {
            RequestType::ProcessMessage => {
                if let Ok(message) = serde_json::from_value::<Message>(request.data) {
                    let echo_message = Message::text(format!("Echo: {}", message.content));
                    Ok(PluginResponse::success(
                        request.id,
                        serde_json::to_value(echo_message)?,
                    ))
                } else {
                    Ok(PluginResponse::error(request.id, "Invalid message data"))
                }
            }
            _ => Ok(PluginResponse::error(
                request.id,
                "Unsupported request type",
            )),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_plugin_registry() {
        let mut registry = PluginRegistry::new();
        let plugin = Box::new(EchoPlugin::new());

        assert!(registry.register(plugin).is_ok());
        assert!(registry.get("echo").is_some());

        let plugins = registry.list();
        assert_eq!(plugins.len(), 1);
        assert_eq!(plugins[0].name, "echo");
    }

    #[tokio::test]
    async fn test_plugin_unregister() {
        let mut registry = PluginRegistry::new();
        let plugin = Box::new(EchoPlugin::new());

        registry.register(plugin).unwrap();
        assert!(registry.unregister("echo").await.is_ok());
        assert!(registry.get("echo").is_none());
    }

    #[test]
    fn test_plugin_permissions() {
        let mut registry = PluginRegistry::new();
        let plugin = Box::new(EchoPlugin::new());

        // Before registering, plugin shouldn't have permissions
        assert!(!registry.has_permission("echo", &Permission::All));

        // After registering, plugin gets all permissions by default
        registry.register(plugin).unwrap();
        assert!(registry.has_permission("echo", &Permission::All));
        assert!(registry.has_permission("echo", &Permission::ReadMessages));

        // Non-existent plugins don't have permissions
        assert!(!registry.has_permission("nonexistent", &Permission::ReadMessages));
    }

    #[tokio::test]
    async fn test_echo_plugin() {
        let plugin = EchoPlugin::new();
        let message = Message::text("Hello, world!");

        let request = PluginRequest {
            id: "test-123".to_string(),
            request_type: RequestType::ProcessMessage,
            data: serde_json::to_value(message).unwrap(),
            metadata: HashMap::new(),
        };

        let response = plugin.process(request).await.unwrap();
        assert!(response.success);

        let echo_message: Message = serde_json::from_value(response.data).unwrap();
        assert_eq!(echo_message.content, "Echo: Hello, world!");
    }

    #[test]
    fn test_plugin_response() {
        let response = PluginResponse::success("test", serde_json::json!({"key": "value"}));
        assert!(response.success);
        assert!(response.error.is_none());

        let error_response = PluginResponse::error("test", "Something went wrong");
        assert!(!error_response.success);
        assert_eq!(
            error_response.error.as_deref(),
            Some("Something went wrong")
        );
    }
}