mojentic 1.2.0

An LLM integration framework for Rust
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
//! Asynchronous event dispatcher.
//!
//! This module provides the `AsyncDispatcher` that manages event processing
//! in a background task, routing events to registered agents via a router.

use crate::event::{Event, TerminateEvent};
use crate::router::Router;
use crate::{MojenticError, Result};
use std::collections::VecDeque;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
use tokio::task::JoinHandle;
use tracing::{debug, info};
use uuid::Uuid;

/// Asynchronous event dispatcher for agent systems.
///
/// The dispatcher manages a queue of events and routes them to registered
/// agents via a router. It runs in a background task and can be stopped
/// gracefully.
///
/// # Examples
///
/// ```ignore
/// use mojentic::async_dispatcher::AsyncDispatcher;
/// use mojentic::router::Router;
///
/// let router = Arc::new(Router::new());
/// let mut dispatcher = AsyncDispatcher::new(router);
///
/// dispatcher.start().await.unwrap();
/// dispatcher.dispatch(my_event);
/// dispatcher.wait_for_empty_queue(Some(Duration::from_secs(10))).await.unwrap();
/// dispatcher.stop().await.unwrap();
/// ```
pub struct AsyncDispatcher {
    router: Arc<Router>,
    event_queue: Arc<Mutex<VecDeque<Box<dyn Event>>>>,
    stop_flag: Arc<AtomicBool>,
    task_handle: Option<JoinHandle<()>>,
    batch_size: usize,
}

impl AsyncDispatcher {
    /// Create a new AsyncDispatcher.
    ///
    /// # Arguments
    ///
    /// * `router` - The router to use for routing events to agents
    pub fn new(router: Arc<Router>) -> Self {
        Self {
            router,
            event_queue: Arc::new(Mutex::new(VecDeque::new())),
            stop_flag: Arc::new(AtomicBool::new(false)),
            task_handle: None,
            batch_size: 5,
        }
    }

    /// Set the batch size (number of events to process per iteration).
    ///
    /// # Arguments
    ///
    /// * `size` - The batch size
    pub fn with_batch_size(mut self, size: usize) -> Self {
        self.batch_size = size;
        self
    }

    /// Start the event dispatch task.
    ///
    /// This spawns a background task that processes events from the queue.
    pub async fn start(&mut self) -> Result<()> {
        if self.task_handle.is_some() {
            return Err(MojenticError::DispatcherError("Dispatcher already started".to_string()));
        }

        debug!("Starting async dispatcher");
        self.stop_flag.store(false, Ordering::Relaxed);

        let router = self.router.clone();
        let queue = self.event_queue.clone();
        let stop_flag = self.stop_flag.clone();
        let batch_size = self.batch_size;

        let handle = tokio::spawn(async move {
            Self::dispatch_loop(router, queue, stop_flag, batch_size).await;
        });

        self.task_handle = Some(handle);
        info!("Async dispatcher started");

        Ok(())
    }

    /// Stop the event dispatch task.
    ///
    /// This signals the background task to stop and waits for it to complete.
    pub async fn stop(&mut self) -> Result<()> {
        if let Some(handle) = self.task_handle.take() {
            debug!("Stopping async dispatcher");
            self.stop_flag.store(true, Ordering::Relaxed);
            handle.await.map_err(|e| {
                MojenticError::DispatcherError(format!("Failed to stop dispatcher: {}", e))
            })?;
            info!("Async dispatcher stopped");
        }

        Ok(())
    }

    /// Dispatch an event to the queue.
    ///
    /// The event will be processed asynchronously by the background task.
    ///
    /// # Arguments
    ///
    /// * `event` - The event to dispatch
    pub fn dispatch(&self, mut event: Box<dyn Event>) {
        // Assign correlation_id if not set
        if event.correlation_id().is_none() {
            event.set_correlation_id(Uuid::new_v4().to_string());
        }

        let queue = self.event_queue.clone();
        tokio::spawn(async move {
            let mut q = queue.lock().await;
            debug!("Dispatching event: {:?}", event);
            q.push_back(event);
        });
    }

    /// Wait for the event queue to become empty.
    ///
    /// # Arguments
    ///
    /// * `timeout` - Optional timeout duration
    ///
    /// # Returns
    ///
    /// `true` if the queue is empty, `false` if timeout was reached
    pub async fn wait_for_empty_queue(&self, timeout: Option<Duration>) -> Result<bool> {
        let start = tokio::time::Instant::now();

        loop {
            let len = {
                let queue = self.event_queue.lock().await;
                queue.len()
            };

            if len == 0 {
                return Ok(true);
            }

            if let Some(timeout_duration) = timeout {
                if start.elapsed() > timeout_duration {
                    return Ok(false);
                }
            }

            tokio::time::sleep(Duration::from_millis(100)).await;
        }
    }

    /// Get the current queue length.
    pub async fn queue_len(&self) -> usize {
        let queue = self.event_queue.lock().await;
        queue.len()
    }

    /// Background dispatch loop.
    async fn dispatch_loop(
        router: Arc<Router>,
        queue: Arc<Mutex<VecDeque<Box<dyn Event>>>>,
        stop_flag: Arc<AtomicBool>,
        batch_size: usize,
    ) {
        while !stop_flag.load(Ordering::Relaxed) {
            for _ in 0..batch_size {
                let event = {
                    let mut q = queue.lock().await;
                    q.pop_front()
                };

                if let Some(event) = event {
                    debug!("Processing event: {:?}", event);

                    // Check for terminate event
                    if event.as_any().is::<TerminateEvent>() {
                        info!("Received TerminateEvent, stopping dispatcher");
                        stop_flag.store(true, Ordering::Relaxed);
                        break;
                    }

                    // Get the event type
                    let type_id = event.as_any().type_id();

                    // Get agents for this event type
                    let agents = router.get_agents(type_id);
                    debug!("Found {} agents for event type", agents.len());

                    // Process event through each agent
                    for agent in agents {
                        debug!("Sending event to agent");
                        match agent.receive_event_async(event.clone_box()).await {
                            Ok(new_events) => {
                                debug!("Agent returned {} events", new_events.len());
                                // Add new events to queue
                                let mut q = queue.lock().await;
                                for new_event in new_events {
                                    q.push_back(new_event);
                                }
                            }
                            Err(e) => {
                                tracing::error!("Agent error processing event: {}", e);
                            }
                        }
                    }
                }
            }

            tokio::time::sleep(Duration::from_millis(100)).await;
        }

        debug!("Dispatch loop exiting");
    }
}

impl Drop for AsyncDispatcher {
    fn drop(&mut self) {
        self.stop_flag.store(true, Ordering::Relaxed);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::agents::BaseAsyncAgent;
    use async_trait::async_trait;
    use serde::{Deserialize, Serialize};
    use std::any::Any;

    #[derive(Debug, Clone, Serialize, Deserialize)]
    struct TestEvent {
        source: String,
        correlation_id: Option<String>,
        data: String,
    }

    impl Event for TestEvent {
        fn source(&self) -> &str {
            &self.source
        }
        fn correlation_id(&self) -> Option<&str> {
            self.correlation_id.as_deref()
        }
        fn set_correlation_id(&mut self, id: String) {
            self.correlation_id = Some(id);
        }
        fn as_any(&self) -> &dyn Any {
            self
        }
        fn clone_box(&self) -> Box<dyn Event> {
            Box::new(self.clone())
        }
    }

    struct CountingAgent {
        count: Arc<Mutex<usize>>,
    }

    #[async_trait]
    impl BaseAsyncAgent for CountingAgent {
        async fn receive_event_async(&self, _event: Box<dyn Event>) -> Result<Vec<Box<dyn Event>>> {
            let mut count = self.count.lock().await;
            *count += 1;
            Ok(vec![])
        }
    }

    #[tokio::test]
    async fn test_dispatcher_new() {
        let router = Arc::new(Router::new());
        let dispatcher = AsyncDispatcher::new(router);
        assert!(dispatcher.task_handle.is_none());
        assert_eq!(dispatcher.batch_size, 5);
    }

    #[tokio::test]
    async fn test_dispatcher_with_batch_size() {
        let router = Arc::new(Router::new());
        let dispatcher = AsyncDispatcher::new(router).with_batch_size(10);
        assert_eq!(dispatcher.batch_size, 10);
    }

    #[tokio::test]
    async fn test_start_and_stop() {
        let router = Arc::new(Router::new());
        let mut dispatcher = AsyncDispatcher::new(router);

        dispatcher.start().await.unwrap();
        assert!(dispatcher.task_handle.is_some());

        dispatcher.stop().await.unwrap();
        assert!(dispatcher.task_handle.is_none());
    }

    #[tokio::test]
    async fn test_start_twice_fails() {
        let router = Arc::new(Router::new());
        let mut dispatcher = AsyncDispatcher::new(router);

        dispatcher.start().await.unwrap();
        let result = dispatcher.start().await;
        assert!(result.is_err());

        dispatcher.stop().await.unwrap();
    }

    #[tokio::test]
    async fn test_dispatch_event() {
        let mut router = Router::new();
        let count = Arc::new(Mutex::new(0));
        let agent = Arc::new(CountingAgent {
            count: count.clone(),
        });

        router.add_route::<TestEvent>(agent);

        let mut dispatcher = AsyncDispatcher::new(Arc::new(router));
        dispatcher.start().await.unwrap();

        let event = Box::new(TestEvent {
            source: "Test".to_string(),
            correlation_id: Some("test-123".to_string()),
            data: "test".to_string(),
        }) as Box<dyn Event>;

        dispatcher.dispatch(event);

        // Wait for processing
        tokio::time::sleep(Duration::from_millis(500)).await;

        let final_count = *count.lock().await;
        assert_eq!(final_count, 1);

        dispatcher.stop().await.unwrap();
    }

    #[tokio::test]
    async fn test_dispatch_assigns_correlation_id() {
        let router = Arc::new(Router::new());
        let mut dispatcher = AsyncDispatcher::new(router);
        dispatcher.start().await.unwrap();

        let event = Box::new(TestEvent {
            source: "Test".to_string(),
            correlation_id: None,
            data: "test".to_string(),
        }) as Box<dyn Event>;

        dispatcher.dispatch(event);

        // Give time for the event to be queued
        tokio::time::sleep(Duration::from_millis(100)).await;

        dispatcher.stop().await.unwrap();
    }

    #[tokio::test]
    async fn test_wait_for_empty_queue() {
        let mut router = Router::new();
        let count = Arc::new(Mutex::new(0));
        let agent = Arc::new(CountingAgent {
            count: count.clone(),
        });

        router.add_route::<TestEvent>(agent);

        let mut dispatcher = AsyncDispatcher::new(Arc::new(router));
        dispatcher.start().await.unwrap();

        let event = Box::new(TestEvent {
            source: "Test".to_string(),
            correlation_id: Some("test-456".to_string()),
            data: "test".to_string(),
        }) as Box<dyn Event>;

        dispatcher.dispatch(event);

        let result = dispatcher.wait_for_empty_queue(Some(Duration::from_secs(2))).await.unwrap();

        assert!(result);
        dispatcher.stop().await.unwrap();
    }

    #[tokio::test]
    async fn test_wait_for_empty_queue_timeout() {
        use std::sync::atomic::{AtomicUsize, Ordering};

        struct SlowAgent {
            processing_count: Arc<AtomicUsize>,
        }

        #[async_trait]
        impl BaseAsyncAgent for SlowAgent {
            async fn receive_event_async(
                &self,
                _event: Box<dyn Event>,
            ) -> Result<Vec<Box<dyn Event>>> {
                // Simulate slow processing
                tokio::time::sleep(Duration::from_millis(200)).await;
                self.processing_count.fetch_add(1, Ordering::Relaxed);
                Ok(vec![])
            }
        }

        let mut router = Router::new();
        let processing_count = Arc::new(AtomicUsize::new(0));
        let agent = Arc::new(SlowAgent {
            processing_count: processing_count.clone(),
        });
        router.add_route::<TestEvent>(agent);

        let mut dispatcher = AsyncDispatcher::new(Arc::new(router));
        dispatcher.start().await.unwrap();

        // Dispatch multiple events that take time to process
        for i in 0..10 {
            let event = Box::new(TestEvent {
                source: "Test".to_string(),
                correlation_id: Some(format!("slow-{}", i)),
                data: "test".to_string(),
            }) as Box<dyn Event>;
            dispatcher.dispatch(event);
        }

        // Give time for events to be queued
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Queue should not be empty within short timeout due to slow processing
        let result =
            dispatcher.wait_for_empty_queue(Some(Duration::from_millis(300))).await.unwrap();

        assert!(!result); // Should timeout before all events are processed

        dispatcher.stop().await.unwrap();
    }

    #[tokio::test]
    async fn test_queue_len() {
        let router = Arc::new(Router::new());
        let mut dispatcher = AsyncDispatcher::new(router);

        assert_eq!(dispatcher.queue_len().await, 0);

        dispatcher.start().await.unwrap();

        let event = Box::new(TestEvent {
            source: "Test".to_string(),
            correlation_id: Some("len-test".to_string()),
            data: "test".to_string(),
        }) as Box<dyn Event>;

        dispatcher.dispatch(event);

        // Give time for the event to be queued
        tokio::time::sleep(Duration::from_millis(100)).await;

        dispatcher.stop().await.unwrap();
    }

    #[tokio::test]
    async fn test_terminate_event_stops_dispatcher() {
        let mut router = Router::new();
        let count = Arc::new(Mutex::new(0));
        let agent = Arc::new(CountingAgent {
            count: count.clone(),
        });

        router.add_route::<TestEvent>(agent.clone());
        router.add_route::<TerminateEvent>(agent);

        let mut dispatcher = AsyncDispatcher::new(Arc::new(router));
        dispatcher.start().await.unwrap();

        // Send normal event
        let event = Box::new(TestEvent {
            source: "Test".to_string(),
            correlation_id: Some("before-stop".to_string()),
            data: "test".to_string(),
        }) as Box<dyn Event>;

        dispatcher.dispatch(event);

        // Send terminate event
        let terminate = Box::new(TerminateEvent::new("System")) as Box<dyn Event>;
        dispatcher.dispatch(terminate);

        // Wait for dispatcher to stop itself
        tokio::time::sleep(Duration::from_secs(1)).await;

        // Dispatcher should have stopped
        assert!(dispatcher.stop_flag.load(Ordering::Relaxed));

        dispatcher.stop().await.unwrap();
    }

    #[tokio::test]
    async fn test_multiple_agents_receive_event() {
        let mut router = Router::new();
        let count1 = Arc::new(Mutex::new(0));
        let count2 = Arc::new(Mutex::new(0));

        let agent1 = Arc::new(CountingAgent {
            count: count1.clone(),
        });
        let agent2 = Arc::new(CountingAgent {
            count: count2.clone(),
        });

        router.add_route::<TestEvent>(agent1);
        router.add_route::<TestEvent>(agent2);

        let mut dispatcher = AsyncDispatcher::new(Arc::new(router));
        dispatcher.start().await.unwrap();

        let event = Box::new(TestEvent {
            source: "Test".to_string(),
            correlation_id: Some("multi-agent".to_string()),
            data: "test".to_string(),
        }) as Box<dyn Event>;

        dispatcher.dispatch(event);

        // Wait for processing
        tokio::time::sleep(Duration::from_millis(500)).await;

        assert_eq!(*count1.lock().await, 1);
        assert_eq!(*count2.lock().await, 1);

        dispatcher.stop().await.unwrap();
    }
}