rustant-plugins 1.0.1

Plugin system for Rustant agent — native dynamic loading and WASM sandboxed plugins
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
//! Hook system — 7 hook points for plugin interception.
//!
//! Hooks allow plugins to intercept and modify agent behavior at defined points.
//! Each hook returns a `HookResult` indicating whether execution should continue or be blocked.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// The 7 hook points in the agent lifecycle.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum HookPoint {
    /// Before a tool is executed.
    BeforeToolExecution,
    /// After a tool has executed.
    AfterToolExecution,
    /// Before an LLM request is sent.
    BeforeLlmRequest,
    /// After an LLM response is received.
    AfterLlmResponse,
    /// When a new session starts.
    OnSessionStart,
    /// When a session ends.
    OnSessionEnd,
    /// When an error occurs.
    OnError,
}

/// Context passed to hooks.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HookContext {
    /// The hook point being triggered.
    pub point: HookPoint,
    /// Name of the relevant tool/provider (if applicable).
    pub name: Option<String>,
    /// Input data (tool args, LLM prompt, etc.).
    pub input: Option<serde_json::Value>,
    /// Output data (tool result, LLM response, etc.).
    pub output: Option<serde_json::Value>,
    /// Error message (for OnError).
    pub error: Option<String>,
    /// Session ID.
    pub session_id: Option<String>,
}

impl HookContext {
    /// Create a context for a tool execution hook.
    pub fn tool(point: HookPoint, name: &str, args: serde_json::Value) -> Self {
        Self {
            point,
            name: Some(name.into()),
            input: Some(args),
            output: None,
            error: None,
            session_id: None,
        }
    }

    /// Create a context for an LLM request/response hook.
    pub fn llm(point: HookPoint, provider: &str, data: serde_json::Value) -> Self {
        let (input, output) = if point == HookPoint::BeforeLlmRequest {
            (Some(data), None)
        } else {
            (None, Some(data))
        };
        Self {
            point,
            name: Some(provider.into()),
            input,
            output,
            error: None,
            session_id: None,
        }
    }

    /// Create a context for session lifecycle hooks.
    pub fn session(point: HookPoint, session_id: &str) -> Self {
        Self {
            point,
            name: None,
            input: None,
            output: None,
            error: None,
            session_id: Some(session_id.into()),
        }
    }

    /// Create a context for error hooks.
    pub fn error(error_message: &str) -> Self {
        Self {
            point: HookPoint::OnError,
            name: None,
            input: None,
            output: None,
            error: Some(error_message.into()),
            session_id: None,
        }
    }
}

/// Result of hook execution.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HookResult {
    /// Allow execution to continue.
    Continue,
    /// Block execution (with reason).
    Block(String),
    /// Continue but with modified context.
    Modified,
}

/// Trait for hook implementations.
pub trait Hook: Send + Sync {
    /// Execute the hook and return a result.
    fn execute(&self, context: &HookContext) -> HookResult;

    /// Display name for logging.
    fn name(&self) -> &str;
}

/// Manages hook registration and firing.
pub struct HookManager {
    hooks: HashMap<HookPoint, Vec<Box<dyn Hook>>>,
}

impl HookManager {
    /// Create a new empty hook manager.
    pub fn new() -> Self {
        Self {
            hooks: HashMap::new(),
        }
    }

    /// Register a hook at a specific point.
    pub fn register(&mut self, point: HookPoint, hook: Box<dyn Hook>) {
        self.hooks.entry(point).or_default().push(hook);
    }

    /// Fire all hooks at a given point.
    /// Returns `HookResult::Continue` if all hooks allow, or the first `Block` result.
    pub fn fire(&self, context: &HookContext) -> HookResult {
        let hooks = match self.hooks.get(&context.point) {
            Some(hooks) => hooks,
            None => return HookResult::Continue,
        };

        let mut result = HookResult::Continue;
        for hook in hooks {
            let hook_result = hook.execute(context);
            match &hook_result {
                HookResult::Block(_) => return hook_result,
                HookResult::Modified => result = HookResult::Modified,
                HookResult::Continue => {}
            }
        }
        result
    }

    /// Number of hooks registered at a specific point.
    pub fn count_at(&self, point: HookPoint) -> usize {
        self.hooks.get(&point).map(|v| v.len()).unwrap_or(0)
    }

    /// Total number of registered hooks.
    pub fn total_hooks(&self) -> usize {
        self.hooks.values().map(|v| v.len()).sum()
    }

    /// Clear all hooks.
    pub fn clear(&mut self) {
        self.hooks.clear();
    }
}

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

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

    struct AllowHook;
    impl Hook for AllowHook {
        fn execute(&self, _context: &HookContext) -> HookResult {
            HookResult::Continue
        }
        fn name(&self) -> &str {
            "allow"
        }
    }

    struct BlockHook {
        reason: String,
    }
    impl BlockHook {
        fn new(reason: &str) -> Self {
            Self {
                reason: reason.into(),
            }
        }
    }
    impl Hook for BlockHook {
        fn execute(&self, _context: &HookContext) -> HookResult {
            HookResult::Block(self.reason.clone())
        }
        fn name(&self) -> &str {
            "block"
        }
    }

    struct ModifyHook;
    impl Hook for ModifyHook {
        fn execute(&self, _context: &HookContext) -> HookResult {
            HookResult::Modified
        }
        fn name(&self) -> &str {
            "modify"
        }
    }

    struct CountingHook {
        name: String,
    }
    impl CountingHook {
        fn new(name: &str) -> Self {
            Self { name: name.into() }
        }
    }
    impl Hook for CountingHook {
        fn execute(&self, _context: &HookContext) -> HookResult {
            HookResult::Continue
        }
        fn name(&self) -> &str {
            &self.name
        }
    }

    #[test]
    fn test_hook_manager_register_and_fire() {
        let mut mgr = HookManager::new();
        mgr.register(HookPoint::BeforeToolExecution, Box::new(AllowHook));

        let ctx = HookContext::tool(
            HookPoint::BeforeToolExecution,
            "shell_exec",
            serde_json::json!({"cmd": "ls"}),
        );
        let result = mgr.fire(&ctx);
        assert_eq!(result, HookResult::Continue);
    }

    #[test]
    fn test_hook_manager_block() {
        let mut mgr = HookManager::new();
        mgr.register(
            HookPoint::BeforeToolExecution,
            Box::new(BlockHook::new("dangerous")),
        );

        let ctx = HookContext::tool(HookPoint::BeforeToolExecution, "rm", serde_json::json!({}));
        let result = mgr.fire(&ctx);
        assert_eq!(result, HookResult::Block("dangerous".into()));
    }

    #[test]
    fn test_hook_ordering_block_stops_chain() {
        let mut mgr = HookManager::new();
        mgr.register(HookPoint::BeforeToolExecution, Box::new(AllowHook));
        mgr.register(
            HookPoint::BeforeToolExecution,
            Box::new(BlockHook::new("blocked")),
        );
        mgr.register(HookPoint::BeforeToolExecution, Box::new(AllowHook));

        let ctx = HookContext::tool(
            HookPoint::BeforeToolExecution,
            "test",
            serde_json::json!({}),
        );
        let result = mgr.fire(&ctx);
        assert_eq!(result, HookResult::Block("blocked".into()));
    }

    #[test]
    fn test_hook_modified_result() {
        let mut mgr = HookManager::new();
        mgr.register(HookPoint::AfterLlmResponse, Box::new(ModifyHook));
        mgr.register(HookPoint::AfterLlmResponse, Box::new(AllowHook));

        let ctx = HookContext::llm(
            HookPoint::AfterLlmResponse,
            "openai",
            serde_json::json!({"text": "hello"}),
        );
        let result = mgr.fire(&ctx);
        assert_eq!(result, HookResult::Modified);
    }

    #[test]
    fn test_hook_fire_no_hooks() {
        let mgr = HookManager::new();
        let ctx = HookContext::session(HookPoint::OnSessionStart, "session-1");
        let result = mgr.fire(&ctx);
        assert_eq!(result, HookResult::Continue);
    }

    #[test]
    fn test_hook_manager_count() {
        let mut mgr = HookManager::new();
        mgr.register(HookPoint::BeforeToolExecution, Box::new(AllowHook));
        mgr.register(HookPoint::BeforeToolExecution, Box::new(AllowHook));
        mgr.register(HookPoint::OnError, Box::new(AllowHook));

        assert_eq!(mgr.count_at(HookPoint::BeforeToolExecution), 2);
        assert_eq!(mgr.count_at(HookPoint::OnError), 1);
        assert_eq!(mgr.count_at(HookPoint::OnSessionEnd), 0);
        assert_eq!(mgr.total_hooks(), 3);
    }

    #[test]
    fn test_hook_manager_clear() {
        let mut mgr = HookManager::new();
        mgr.register(HookPoint::BeforeToolExecution, Box::new(AllowHook));
        mgr.register(HookPoint::OnError, Box::new(AllowHook));
        assert_eq!(mgr.total_hooks(), 2);

        mgr.clear();
        assert_eq!(mgr.total_hooks(), 0);
    }

    #[test]
    fn test_hook_context_tool() {
        let ctx = HookContext::tool(
            HookPoint::BeforeToolExecution,
            "shell_exec",
            serde_json::json!({"cmd": "ls"}),
        );
        assert_eq!(ctx.point, HookPoint::BeforeToolExecution);
        assert_eq!(ctx.name.as_deref(), Some("shell_exec"));
        assert!(ctx.input.is_some());
    }

    #[test]
    fn test_hook_context_error() {
        let ctx = HookContext::error("something went wrong");
        assert_eq!(ctx.point, HookPoint::OnError);
        assert_eq!(ctx.error.as_deref(), Some("something went wrong"));
    }

    #[test]
    fn test_multiple_hooks_fire_in_order() {
        let mut mgr = HookManager::new();
        mgr.register(
            HookPoint::BeforeToolExecution,
            Box::new(CountingHook::new("first")),
        );
        mgr.register(
            HookPoint::BeforeToolExecution,
            Box::new(CountingHook::new("second")),
        );
        mgr.register(
            HookPoint::BeforeToolExecution,
            Box::new(CountingHook::new("third")),
        );

        // All continue, so result should be Continue
        let ctx = HookContext::tool(
            HookPoint::BeforeToolExecution,
            "test",
            serde_json::json!({}),
        );
        assert_eq!(mgr.fire(&ctx), HookResult::Continue);
        assert_eq!(mgr.count_at(HookPoint::BeforeToolExecution), 3);
    }

    #[test]
    fn test_hook_point_serialization() {
        let point = HookPoint::BeforeToolExecution;
        let json = serde_json::to_string(&point).unwrap();
        let restored: HookPoint = serde_json::from_str(&json).unwrap();
        assert_eq!(restored, HookPoint::BeforeToolExecution);
    }

    #[test]
    fn test_all_seven_hook_points() {
        let points = vec![
            HookPoint::BeforeToolExecution,
            HookPoint::AfterToolExecution,
            HookPoint::BeforeLlmRequest,
            HookPoint::AfterLlmResponse,
            HookPoint::OnSessionStart,
            HookPoint::OnSessionEnd,
            HookPoint::OnError,
        ];
        assert_eq!(points.len(), 7);

        // Each can be used as a hash key
        let mut mgr = HookManager::new();
        for point in &points {
            mgr.register(*point, Box::new(AllowHook));
        }
        assert_eq!(mgr.total_hooks(), 7);
    }
}