praisonai 0.2.0

Core library for PraisonAI - Agent, Tools, Workflows
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Telemetry Module for PraisonAI Rust SDK.
//!
//! Provides performance monitoring and telemetry capabilities.
//!
//! # Example
//!
//! ```ignore
//! use praisonai::telemetry::{PerformanceMonitor, FunctionStats};
//!
//! let monitor = PerformanceMonitor::new();
//! monitor.track_function("my_function", Duration::from_millis(100));
//! let stats = monitor.get_stats("my_function");
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};

// =============================================================================
// FUNCTION STATS
// =============================================================================

/// Statistics for a tracked function.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionStats {
    /// Function name
    pub name: String,
    /// Number of calls
    pub call_count: usize,
    /// Total duration
    pub total_duration: Duration,
    /// Minimum duration
    pub min_duration: Duration,
    /// Maximum duration
    pub max_duration: Duration,
    /// Last call duration
    pub last_duration: Duration,
}

impl FunctionStats {
    /// Create new stats for a function.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            call_count: 0,
            total_duration: Duration::ZERO,
            min_duration: Duration::MAX,
            max_duration: Duration::ZERO,
            last_duration: Duration::ZERO,
        }
    }

    /// Record a call.
    pub fn record(&mut self, duration: Duration) {
        self.call_count += 1;
        self.total_duration += duration;
        self.last_duration = duration;
        
        if duration < self.min_duration {
            self.min_duration = duration;
        }
        if duration > self.max_duration {
            self.max_duration = duration;
        }
    }

    /// Get average duration.
    pub fn average_duration(&self) -> Duration {
        if self.call_count == 0 {
            Duration::ZERO
        } else {
            self.total_duration / self.call_count as u32
        }
    }

    /// Get calls per second.
    pub fn calls_per_second(&self, elapsed: Duration) -> f64 {
        if elapsed.as_secs_f64() == 0.0 {
            0.0
        } else {
            self.call_count as f64 / elapsed.as_secs_f64()
        }
    }
}

// =============================================================================
// API STATS
// =============================================================================

/// Statistics for API calls.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiStats {
    /// Endpoint
    pub endpoint: String,
    /// Number of calls
    pub call_count: usize,
    /// Successful calls
    pub success_count: usize,
    /// Failed calls
    pub error_count: usize,
    /// Total duration
    pub total_duration: Duration,
    /// Status code counts
    pub status_codes: HashMap<u16, usize>,
}

impl ApiStats {
    /// Create new API stats.
    pub fn new(endpoint: impl Into<String>) -> Self {
        Self {
            endpoint: endpoint.into(),
            call_count: 0,
            success_count: 0,
            error_count: 0,
            total_duration: Duration::ZERO,
            status_codes: HashMap::new(),
        }
    }

    /// Record a successful call.
    pub fn record_success(&mut self, duration: Duration, status_code: u16) {
        self.call_count += 1;
        self.success_count += 1;
        self.total_duration += duration;
        *self.status_codes.entry(status_code).or_insert(0) += 1;
    }

    /// Record a failed call.
    pub fn record_error(&mut self, duration: Duration, status_code: Option<u16>) {
        self.call_count += 1;
        self.error_count += 1;
        self.total_duration += duration;
        if let Some(code) = status_code {
            *self.status_codes.entry(code).or_insert(0) += 1;
        }
    }

    /// Get success rate.
    pub fn success_rate(&self) -> f64 {
        if self.call_count == 0 {
            1.0
        } else {
            self.success_count as f64 / self.call_count as f64
        }
    }

    /// Get average duration.
    pub fn average_duration(&self) -> Duration {
        if self.call_count == 0 {
            Duration::ZERO
        } else {
            self.total_duration / self.call_count as u32
        }
    }
}

// =============================================================================
// PERFORMANCE MONITOR
// =============================================================================

/// Performance monitor for tracking function and API performance.
#[derive(Debug)]
pub struct PerformanceMonitor {
    /// Function statistics
    functions: Arc<RwLock<HashMap<String, FunctionStats>>>,
    /// API statistics
    apis: Arc<RwLock<HashMap<String, ApiStats>>>,
    /// Start time
    start_time: Instant,
    /// Whether monitoring is enabled
    enabled: bool,
}

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

impl PerformanceMonitor {
    /// Create a new monitor.
    pub fn new() -> Self {
        Self {
            functions: Arc::new(RwLock::new(HashMap::new())),
            apis: Arc::new(RwLock::new(HashMap::new())),
            start_time: Instant::now(),
            enabled: true,
        }
    }

    /// Enable monitoring.
    pub fn enable(&mut self) {
        self.enabled = true;
    }

    /// Disable monitoring.
    pub fn disable(&mut self) {
        self.enabled = false;
    }

    /// Check if enabled.
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    /// Track a function call.
    pub fn track_function(&self, name: &str, duration: Duration) {
        if !self.enabled {
            return;
        }

        let mut functions = self.functions.write().unwrap();
        functions
            .entry(name.to_string())
            .or_insert_with(|| FunctionStats::new(name))
            .record(duration);
    }

    /// Track an API call.
    pub fn track_api(&self, endpoint: &str, duration: Duration, success: bool, status_code: Option<u16>) {
        if !self.enabled {
            return;
        }

        let mut apis = self.apis.write().unwrap();
        let stats = apis
            .entry(endpoint.to_string())
            .or_insert_with(|| ApiStats::new(endpoint));

        if success {
            stats.record_success(duration, status_code.unwrap_or(200));
        } else {
            stats.record_error(duration, status_code);
        }
    }

    /// Get function stats.
    pub fn get_function_stats(&self, name: &str) -> Option<FunctionStats> {
        self.functions.read().unwrap().get(name).cloned()
    }

    /// Get API stats.
    pub fn get_api_stats(&self, endpoint: &str) -> Option<ApiStats> {
        self.apis.read().unwrap().get(endpoint).cloned()
    }

    /// Get all function stats.
    pub fn all_function_stats(&self) -> Vec<FunctionStats> {
        self.functions.read().unwrap().values().cloned().collect()
    }

    /// Get all API stats.
    pub fn all_api_stats(&self) -> Vec<ApiStats> {
        self.apis.read().unwrap().values().cloned().collect()
    }

    /// Get slowest functions.
    pub fn slowest_functions(&self, limit: usize) -> Vec<FunctionStats> {
        let mut stats: Vec<_> = self.all_function_stats();
        stats.sort_by(|a, b| b.average_duration().cmp(&a.average_duration()));
        stats.truncate(limit);
        stats
    }

    /// Get slowest APIs.
    pub fn slowest_apis(&self, limit: usize) -> Vec<ApiStats> {
        let mut stats: Vec<_> = self.all_api_stats();
        stats.sort_by(|a, b| b.average_duration().cmp(&a.average_duration()));
        stats.truncate(limit);
        stats
    }

    /// Get elapsed time since start.
    pub fn elapsed(&self) -> Duration {
        self.start_time.elapsed()
    }

    /// Clear all data.
    pub fn clear(&self) {
        self.functions.write().unwrap().clear();
        self.apis.write().unwrap().clear();
    }

    /// Get performance report.
    pub fn get_report(&self) -> PerformanceReport {
        PerformanceReport {
            elapsed: self.elapsed(),
            function_count: self.functions.read().unwrap().len(),
            api_count: self.apis.read().unwrap().len(),
            total_function_calls: self.functions.read().unwrap().values().map(|s| s.call_count).sum(),
            total_api_calls: self.apis.read().unwrap().values().map(|s| s.call_count).sum(),
            slowest_functions: self.slowest_functions(5),
            slowest_apis: self.slowest_apis(5),
        }
    }
}

/// Performance report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceReport {
    /// Elapsed time
    pub elapsed: Duration,
    /// Number of tracked functions
    pub function_count: usize,
    /// Number of tracked APIs
    pub api_count: usize,
    /// Total function calls
    pub total_function_calls: usize,
    /// Total API calls
    pub total_api_calls: usize,
    /// Slowest functions
    pub slowest_functions: Vec<FunctionStats>,
    /// Slowest APIs
    pub slowest_apis: Vec<ApiStats>,
}

// =============================================================================
// TELEMETRY COLLECTOR
// =============================================================================

/// Event types for telemetry.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TelemetryEventType {
    AgentStart,
    AgentEnd,
    ToolCall,
    LlmRequest,
    LlmResponse,
    Error,
    Custom(String),
}

/// A telemetry event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TelemetryEvent {
    /// Event type
    pub event_type: TelemetryEventType,
    /// Timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,
    /// Event data
    pub data: HashMap<String, serde_json::Value>,
    /// Duration (if applicable)
    pub duration: Option<Duration>,
}

impl TelemetryEvent {
    /// Create a new event.
    pub fn new(event_type: TelemetryEventType) -> Self {
        Self {
            event_type,
            timestamp: chrono::Utc::now(),
            data: HashMap::new(),
            duration: None,
        }
    }

    /// Add data.
    pub fn with_data(mut self, key: impl Into<String>, value: impl Serialize) -> Self {
        self.data.insert(key.into(), serde_json::to_value(value).unwrap_or_default());
        self
    }

    /// Set duration.
    pub fn with_duration(mut self, duration: Duration) -> Self {
        self.duration = Some(duration);
        self
    }
}

/// Telemetry collector.
#[derive(Debug, Default)]
pub struct TelemetryCollector {
    /// Collected events
    events: Arc<RwLock<Vec<TelemetryEvent>>>,
    /// Whether collection is enabled
    enabled: bool,
    /// Maximum events to keep
    max_events: usize,
}

impl TelemetryCollector {
    /// Create a new collector.
    pub fn new() -> Self {
        Self {
            events: Arc::new(RwLock::new(Vec::new())),
            enabled: true,
            max_events: 10000,
        }
    }

    /// Set max events.
    pub fn with_max_events(mut self, max: usize) -> Self {
        self.max_events = max;
        self
    }

    /// Enable collection.
    pub fn enable(&mut self) {
        self.enabled = true;
    }

    /// Disable collection.
    pub fn disable(&mut self) {
        self.enabled = false;
    }

    /// Record an event.
    pub fn record(&self, event: TelemetryEvent) {
        if !self.enabled {
            return;
        }

        let mut events = self.events.write().unwrap();
        events.push(event);

        // Trim if over limit
        if events.len() > self.max_events {
            let excess = events.len() - self.max_events;
            events.drain(0..excess);
        }
    }

    /// Get all events.
    pub fn events(&self) -> Vec<TelemetryEvent> {
        self.events.read().unwrap().clone()
    }

    /// Get events by type.
    pub fn events_by_type(&self, event_type: &TelemetryEventType) -> Vec<TelemetryEvent> {
        self.events
            .read()
            .unwrap()
            .iter()
            .filter(|e| std::mem::discriminant(&e.event_type) == std::mem::discriminant(event_type))
            .cloned()
            .collect()
    }

    /// Get event count.
    pub fn event_count(&self) -> usize {
        self.events.read().unwrap().len()
    }

    /// Clear all events.
    pub fn clear(&self) {
        self.events.write().unwrap().clear();
    }
}

// =============================================================================
// GLOBAL INSTANCES
// =============================================================================

use std::sync::OnceLock;

static GLOBAL_MONITOR: OnceLock<PerformanceMonitor> = OnceLock::new();
static GLOBAL_COLLECTOR: OnceLock<TelemetryCollector> = OnceLock::new();

/// Get the global performance monitor.
pub fn get_monitor() -> &'static PerformanceMonitor {
    GLOBAL_MONITOR.get_or_init(PerformanceMonitor::new)
}

/// Get the global telemetry collector.
pub fn get_collector() -> &'static TelemetryCollector {
    GLOBAL_COLLECTOR.get_or_init(TelemetryCollector::new)
}

/// Track a function call on the global monitor.
pub fn track_function(name: &str, duration: Duration) {
    get_monitor().track_function(name, duration);
}

/// Track an API call on the global monitor.
pub fn track_api(endpoint: &str, duration: Duration, success: bool, status_code: Option<u16>) {
    get_monitor().track_api(endpoint, duration, success, status_code);
}

/// Record a telemetry event.
pub fn record_event(event: TelemetryEvent) {
    get_collector().record(event);
}

/// Get the global performance report.
pub fn get_performance_report() -> PerformanceReport {
    get_monitor().get_report()
}

// =============================================================================
// TESTS
// =============================================================================

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

    #[test]
    fn test_function_stats() {
        let mut stats = FunctionStats::new("test");
        stats.record(Duration::from_millis(100));
        stats.record(Duration::from_millis(200));

        assert_eq!(stats.call_count, 2);
        assert_eq!(stats.min_duration, Duration::from_millis(100));
        assert_eq!(stats.max_duration, Duration::from_millis(200));
        assert_eq!(stats.average_duration(), Duration::from_millis(150));
    }

    #[test]
    fn test_api_stats() {
        let mut stats = ApiStats::new("/api/test");
        stats.record_success(Duration::from_millis(100), 200);
        stats.record_success(Duration::from_millis(150), 200);
        stats.record_error(Duration::from_millis(50), Some(500));

        assert_eq!(stats.call_count, 3);
        assert_eq!(stats.success_count, 2);
        assert_eq!(stats.error_count, 1);
        assert!((stats.success_rate() - 0.666).abs() < 0.01);
    }

    #[test]
    fn test_performance_monitor() {
        let monitor = PerformanceMonitor::new();
        
        monitor.track_function("func1", Duration::from_millis(100));
        monitor.track_function("func1", Duration::from_millis(200));
        monitor.track_function("func2", Duration::from_millis(50));

        let stats = monitor.get_function_stats("func1").unwrap();
        assert_eq!(stats.call_count, 2);

        let slowest = monitor.slowest_functions(1);
        assert_eq!(slowest.len(), 1);
        assert_eq!(slowest[0].name, "func1");
    }

    #[test]
    fn test_performance_monitor_disabled() {
        let mut monitor = PerformanceMonitor::new();
        monitor.disable();
        
        monitor.track_function("func1", Duration::from_millis(100));
        
        assert!(monitor.get_function_stats("func1").is_none());
    }

    #[test]
    fn test_telemetry_event() {
        let event = TelemetryEvent::new(TelemetryEventType::AgentStart)
            .with_data("agent_name", "test-agent")
            .with_duration(Duration::from_secs(1));

        assert!(event.data.contains_key("agent_name"));
        assert_eq!(event.duration, Some(Duration::from_secs(1)));
    }

    #[test]
    fn test_telemetry_collector() {
        let collector = TelemetryCollector::new();
        
        collector.record(TelemetryEvent::new(TelemetryEventType::AgentStart));
        collector.record(TelemetryEvent::new(TelemetryEventType::ToolCall));
        
        assert_eq!(collector.event_count(), 2);
        
        let agent_events = collector.events_by_type(&TelemetryEventType::AgentStart);
        assert_eq!(agent_events.len(), 1);
    }

    #[test]
    fn test_telemetry_collector_max_events() {
        let collector = TelemetryCollector::new().with_max_events(5);
        
        for _ in 0..10 {
            collector.record(TelemetryEvent::new(TelemetryEventType::ToolCall));
        }
        
        assert_eq!(collector.event_count(), 5);
    }

    #[test]
    fn test_performance_report() {
        let monitor = PerformanceMonitor::new();
        monitor.track_function("func1", Duration::from_millis(100));
        monitor.track_api("/api/test", Duration::from_millis(200), true, Some(200));
        
        let report = monitor.get_report();
        assert_eq!(report.function_count, 1);
        assert_eq!(report.api_count, 1);
        assert_eq!(report.total_function_calls, 1);
        assert_eq!(report.total_api_calls, 1);
    }
}