orra 0.0.2

Context-aware agent session management for any application
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
//! Plugin system.
//!
//! Allows extending the agent with dynamically loaded plugins that can
//! register tools, hooks, and providers. Plugins are defined by implementing
//! the `Plugin` trait and can be loaded from a plugin registry.

use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;

use crate::hook::HookRegistry;
use crate::tool::ToolRegistry;

// ---------------------------------------------------------------------------
// Plugin trait
// ---------------------------------------------------------------------------

/// A plugin that extends the agent with additional capabilities.
///
/// Plugins are initialized once during startup and can register tools,
/// hooks, and configuration. They provide a clean way to package related
/// functionality into reusable modules.
#[async_trait]
pub trait Plugin: Send + Sync {
    /// Unique name identifying this plugin.
    fn name(&self) -> &str;

    /// Human-readable description of what this plugin does.
    fn description(&self) -> &str;

    /// Semantic version of this plugin.
    fn version(&self) -> &str;

    /// Initialize the plugin. Called once during startup before
    /// tools and hooks are registered.
    async fn init(&mut self, config: &PluginConfig) -> Result<(), PluginError>;

    /// Register any tools this plugin provides.
    fn register_tools(&self, registry: &mut ToolRegistry);

    /// Register any hooks this plugin provides.
    fn register_hooks(&self, registry: &mut HookRegistry);

    /// Shut down the plugin. Called during graceful shutdown.
    async fn shutdown(&self) -> Result<(), PluginError> {
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Plugin configuration
// ---------------------------------------------------------------------------

/// Configuration passed to a plugin during initialization.
#[derive(Debug, Clone, Default)]
pub struct PluginConfig {
    /// Key-value settings specific to this plugin, typically loaded
    /// from the application's config file.
    pub settings: HashMap<String, serde_json::Value>,
}

impl PluginConfig {
    pub fn new() -> Self {
        Self::default()
    }

    /// Get a string setting.
    pub fn get_str(&self, key: &str) -> Option<&str> {
        self.settings.get(key).and_then(|v| v.as_str())
    }

    /// Get a boolean setting.
    pub fn get_bool(&self, key: &str) -> Option<bool> {
        self.settings.get(key).and_then(|v| v.as_bool())
    }

    /// Get an integer setting.
    pub fn get_i64(&self, key: &str) -> Option<i64> {
        self.settings.get(key).and_then(|v| v.as_i64())
    }

    /// Set a value.
    pub fn set(&mut self, key: impl Into<String>, value: serde_json::Value) {
        self.settings.insert(key.into(), value);
    }
}

// ---------------------------------------------------------------------------
// Plugin registry
// ---------------------------------------------------------------------------

/// Manages a collection of plugins, handling initialization, tool/hook
/// registration, and shutdown.
pub struct PluginRegistry {
    plugins: Vec<PluginEntry>,
    configs: HashMap<String, PluginConfig>,
}

struct PluginEntry {
    plugin: Box<dyn Plugin>,
    enabled: bool,
    initialized: bool,
}

impl PluginRegistry {
    pub fn new() -> Self {
        Self {
            plugins: Vec::new(),
            configs: HashMap::new(),
        }
    }

    /// Register a plugin. It won't be initialized until `init_all()` is called.
    pub fn register(&mut self, plugin: Box<dyn Plugin>) {
        self.plugins.push(PluginEntry {
            plugin,
            enabled: true,
            initialized: false,
        });
    }

    /// Set configuration for a plugin by name.
    pub fn set_config(&mut self, name: impl Into<String>, config: PluginConfig) {
        self.configs.insert(name.into(), config);
    }

    /// Enable or disable a plugin by name.
    pub fn set_enabled(&mut self, name: &str, enabled: bool) -> bool {
        for entry in &mut self.plugins {
            if entry.plugin.name() == name {
                entry.enabled = enabled;
                return true;
            }
        }
        false
    }

    /// Initialize all enabled plugins.
    pub async fn init_all(&mut self) -> Result<Vec<String>, PluginError> {
        let mut initialized = Vec::new();

        for entry in &mut self.plugins {
            if !entry.enabled {
                continue;
            }

            let config = self
                .configs
                .get(entry.plugin.name())
                .cloned()
                .unwrap_or_default();

            entry
                .plugin
                .init(&config)
                .await
                .map_err(|e| PluginError::InitFailed {
                    plugin: entry.plugin.name().to_string(),
                    cause: e.to_string(),
                })?;

            entry.initialized = true;
            initialized.push(entry.plugin.name().to_string());
        }

        Ok(initialized)
    }

    /// Register tools from all initialized plugins.
    pub fn register_all_tools(&self, registry: &mut ToolRegistry) {
        for entry in &self.plugins {
            if entry.enabled && entry.initialized {
                entry.plugin.register_tools(registry);
            }
        }
    }

    /// Register hooks from all initialized plugins.
    pub fn register_all_hooks(&self, registry: &mut HookRegistry) {
        for entry in &self.plugins {
            if entry.enabled && entry.initialized {
                entry.plugin.register_hooks(registry);
            }
        }
    }

    /// Shut down all initialized plugins.
    pub async fn shutdown_all(&self) -> Vec<(String, Result<(), PluginError>)> {
        let mut results = Vec::new();

        for entry in self.plugins.iter().rev() {
            if entry.initialized {
                let name = entry.plugin.name().to_string();
                let result = entry.plugin.shutdown().await;
                results.push((name, result));
            }
        }

        results
    }

    /// List all registered plugins.
    pub fn list(&self) -> Vec<PluginInfo> {
        self.plugins
            .iter()
            .map(|entry| PluginInfo {
                name: entry.plugin.name().to_string(),
                description: entry.plugin.description().to_string(),
                version: entry.plugin.version().to_string(),
                enabled: entry.enabled,
                initialized: entry.initialized,
            })
            .collect()
    }

    /// Get the number of registered plugins.
    pub fn len(&self) -> usize {
        self.plugins.len()
    }

    pub fn is_empty(&self) -> bool {
        self.plugins.is_empty()
    }
}

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

// ---------------------------------------------------------------------------
// Plugin info
// ---------------------------------------------------------------------------

/// Public information about a registered plugin.
#[derive(Debug, Clone, serde::Serialize)]
pub struct PluginInfo {
    pub name: String,
    pub description: String,
    pub version: String,
    pub enabled: bool,
    pub initialized: bool,
}

// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------

#[derive(Debug, thiserror::Error)]
pub enum PluginError {
    #[error("plugin '{plugin}' failed to initialize: {cause}")]
    InitFailed { plugin: String, cause: String },

    #[error("plugin error: {0}")]
    Other(String),
}

// Display is derived by thiserror::Error above.

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    struct TestPlugin {
        name: String,
        init_called: Arc<std::sync::atomic::AtomicBool>,
        shutdown_called: Arc<std::sync::atomic::AtomicBool>,
    }

    impl TestPlugin {
        fn new(name: &str) -> Self {
            Self {
                name: name.to_string(),
                init_called: Arc::new(std::sync::atomic::AtomicBool::new(false)),
                shutdown_called: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            }
        }
    }

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

        fn description(&self) -> &str {
            "A test plugin"
        }

        fn version(&self) -> &str {
            "1.0.0"
        }

        async fn init(&mut self, _config: &PluginConfig) -> Result<(), PluginError> {
            self.init_called
                .store(true, std::sync::atomic::Ordering::SeqCst);
            Ok(())
        }

        fn register_tools(&self, _registry: &mut ToolRegistry) {
            // No tools in test plugin
        }

        fn register_hooks(&self, _registry: &mut HookRegistry) {
            // No hooks in test plugin
        }

        async fn shutdown(&self) -> Result<(), PluginError> {
            self.shutdown_called
                .store(true, std::sync::atomic::Ordering::SeqCst);
            Ok(())
        }
    }

    #[test]
    fn plugin_config_get_set() {
        let mut config = PluginConfig::new();
        config.set("api_key", serde_json::json!("sk-123"));
        config.set("enabled", serde_json::json!(true));
        config.set("max_retries", serde_json::json!(3));

        assert_eq!(config.get_str("api_key"), Some("sk-123"));
        assert_eq!(config.get_bool("enabled"), Some(true));
        assert_eq!(config.get_i64("max_retries"), Some(3));
        assert_eq!(config.get_str("missing"), None);
    }

    #[tokio::test]
    async fn registry_init_and_list() {
        let mut registry = PluginRegistry::new();
        registry.register(Box::new(TestPlugin::new("test-1")));
        registry.register(Box::new(TestPlugin::new("test-2")));

        assert_eq!(registry.len(), 2);

        let initialized = registry.init_all().await.unwrap();
        assert_eq!(initialized.len(), 2);

        let list = registry.list();
        assert_eq!(list.len(), 2);
        assert!(list[0].initialized);
        assert!(list[1].initialized);
    }

    #[tokio::test]
    async fn disabled_plugin_not_initialized() {
        let mut registry = PluginRegistry::new();
        registry.register(Box::new(TestPlugin::new("active")));
        registry.register(Box::new(TestPlugin::new("disabled")));
        registry.set_enabled("disabled", false);

        let initialized = registry.init_all().await.unwrap();
        assert_eq!(initialized, vec!["active"]);

        let list = registry.list();
        let disabled = list.iter().find(|p| p.name == "disabled").unwrap();
        assert!(!disabled.initialized);
        assert!(!disabled.enabled);
    }

    #[tokio::test]
    async fn shutdown_all_plugins() {
        let mut registry = PluginRegistry::new();
        registry.register(Box::new(TestPlugin::new("p1")));
        registry.register(Box::new(TestPlugin::new("p2")));

        registry.init_all().await.unwrap();
        let results = registry.shutdown_all().await;

        assert_eq!(results.len(), 2);
        for (_, result) in &results {
            assert!(result.is_ok());
        }
    }

    #[test]
    fn empty_registry() {
        let registry = PluginRegistry::new();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn set_enabled_returns_false_for_unknown() {
        let mut registry = PluginRegistry::new();
        assert!(!registry.set_enabled("nonexistent", false));
    }

    #[test]
    fn plugin_info_serialization() {
        let info = PluginInfo {
            name: "test".into(),
            description: "A test".into(),
            version: "1.0.0".into(),
            enabled: true,
            initialized: false,
        };

        let json = serde_json::to_string(&info).unwrap();
        assert!(json.contains("\"name\":\"test\""));
        assert!(json.contains("\"version\":\"1.0.0\""));
    }

    #[tokio::test]
    async fn plugin_with_config() {
        let mut registry = PluginRegistry::new();
        registry.register(Box::new(TestPlugin::new("configured")));

        let mut config = PluginConfig::new();
        config.set("api_key", serde_json::json!("secret"));
        registry.set_config("configured", config);

        let initialized = registry.init_all().await.unwrap();
        assert_eq!(initialized, vec!["configured"]);
    }
}