paladin-ai-core 0.5.1

Pure domain types for the Paladin framework — zero infrastructure dependencies
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
/*
Logging Container

The Log Container is a type of Node that has a UUID with a vector of LogEntry items.
The Log can be abstracted in the application layer so that multiple Logs may exist.

By defining only the most fundamental requirements within the Core we allow the application
to handle all the details that it requires while still enabling the other containers and
services at this layer to meet their requirements. The event_manager, messaging_service,
etc. require at least one log to be defined.

The Log Entry Container is a type of Message that an Event will Trigger. It contains the
log message, the log level, and the timestamp when the log entry was created.

The Config Service Settings requires at least one log location to be defined.
*/
use crate::base::entity::message::{Location, Message, MessagePriority};
use crate::base::entity::node::Node;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};

/// Log severity levels
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default,
)]
pub enum LogLevel {
    Trace = 0,
    Debug = 1,
    #[default]
    Info = 2,
    Warn = 3,
    Error = 4,
    Fatal = 5,
}

impl LogLevel {
    /// Convert to string representation
    pub fn as_str(&self) -> &'static str {
        match self {
            LogLevel::Trace => "TRACE",
            LogLevel::Debug => "DEBUG",
            LogLevel::Info => "INFO",
            LogLevel::Warn => "WARN",
            LogLevel::Error => "ERROR",
            LogLevel::Fatal => "FATAL",
        }
    }

    /// Parse from string
    pub fn parse_str(s: &str) -> Option<Self> {
        match s.to_uppercase().as_str() {
            "TRACE" => Some(LogLevel::Trace),
            "DEBUG" => Some(LogLevel::Debug),
            "INFO" => Some(LogLevel::Info),
            "WARN" | "WARNING" => Some(LogLevel::Warn),
            "ERROR" => Some(LogLevel::Error),
            "FATAL" => Some(LogLevel::Fatal),
            _ => None,
        }
    }

    /// Convert to message priority
    pub fn to_priority(&self) -> MessagePriority {
        match self {
            LogLevel::Trace | LogLevel::Debug => MessagePriority::Low,
            LogLevel::Info => MessagePriority::Normal,
            LogLevel::Warn => MessagePriority::High,
            LogLevel::Error | LogLevel::Fatal => MessagePriority::Critical,
        }
    }
}

/// Log destinations for routing log entries
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum LogDestination {
    /// General system operations log
    System,
    /// HTTP access and API requests
    Access,
    /// Error conditions and exceptions
    Error,
    /// Security-related events
    Security,
    /// Performance and metrics data
    Performance,
    /// Custom log destination
    Custom(String),
}

impl LogDestination {
    /// Convert to Location for message routing
    pub fn to_location(&self) -> Location {
        match self {
            LogDestination::System => Location::service("system-log"),
            LogDestination::Access => Location::service("access-log"),
            LogDestination::Error => Location::service("error-log"),
            LogDestination::Security => Location::service("security-log"),
            LogDestination::Performance => Location::service("performance-log"),
            LogDestination::Custom(name) => Location::service(&format!("custom-log-{}", name)),
        }
    }

    /// Get the log destination name
    pub fn name(&self) -> String {
        match self {
            LogDestination::System => "system".to_string(),
            LogDestination::Access => "access".to_string(),
            LogDestination::Error => "error".to_string(),
            LogDestination::Security => "security".to_string(),
            LogDestination::Performance => "performance".to_string(),
            LogDestination::Custom(name) => name.clone(),
        }
    }
}

/// Log message content
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct LogMessage {
    /// Log severity level
    pub level: LogLevel,
    /// The actual log message
    pub message: String,
    /// Optional module or component that generated the log
    pub module: Option<String>,
    /// Optional function or method name
    pub function: Option<String>,
    /// Optional file name and line number
    pub location: Option<String>,
    /// Optional additional context data
    pub context: Option<serde_json::Value>,
}

impl LogMessage {
    /// Create a new log message
    pub fn new(level: LogLevel, message: String) -> Self {
        Self {
            level,
            message,
            module: None,
            function: None,
            location: None,
            context: None,
        }
    }

    /// Create with module information
    pub fn with_module(mut self, module: String) -> Self {
        self.module = Some(module);
        self
    }

    /// Create with function information
    pub fn with_function(mut self, function: String) -> Self {
        self.function = Some(function);
        self
    }

    /// Create with location information
    pub fn with_location(mut self, location: String) -> Self {
        self.location = Some(location);
        self
    }

    /// Create with context data
    pub fn with_context(mut self, context: serde_json::Value) -> Self {
        self.context = Some(context);
        self
    }

    /// Get formatted message for display
    pub fn formatted(&self) -> String {
        let mut parts = vec![format!("[{}]", self.level.as_str()), self.message.clone()];

        if let Some(module) = &self.module {
            parts.push(format!("module:{}", module));
        }

        if let Some(function) = &self.function {
            parts.push(format!("fn:{}", function));
        }

        if let Some(location) = &self.location {
            parts.push(format!("at:{}", location));
        }

        parts.join(" ")
    }
}

/// Type alias for log entries - Messages containing LogMessage
pub type LogEntry = Message<LogMessage>;

/// Helper functions for creating LogEntry instances
pub struct LogEntryBuilder;

impl LogEntryBuilder {
    /// Create a new log entry
    pub fn new_entry(
        source: Location,
        destination: LogDestination,
        level: LogLevel,
        message: String,
    ) -> LogEntry {
        let log_message = LogMessage::new(level, message);
        let priority = level.to_priority();

        Message::with_priority(source, destination.to_location(), log_message, priority)
    }

    /// Create a log entry with additional context using builder pattern
    ///
    /// # Arguments
    /// * `context` - Context builder containing all optional fields
    #[allow(clippy::too_many_arguments)]
    pub fn new_entry_with_context(
        source: Location,
        destination: LogDestination,
        level: LogLevel,
        message: String,
        module: Option<String>,
        function: Option<String>,
        location: Option<String>,
        context: Option<serde_json::Value>,
    ) -> LogEntry {
        let mut log_message = LogMessage::new(level, message);

        if let Some(module) = module {
            log_message = log_message.with_module(module);
        }

        if let Some(function) = function {
            log_message = log_message.with_function(function);
        }

        if let Some(location) = location {
            log_message = log_message.with_location(location);
        }

        if let Some(context) = context {
            log_message = log_message.with_context(context);
        }

        let priority = level.to_priority();

        Message::with_priority(source, destination.to_location(), log_message, priority)
    }
}

/// Extension trait for LogEntry to add log-specific functionality
pub trait LogEntryExt {
    /// Get the log level of this entry
    fn level(&self) -> LogLevel;

    /// Get the formatted message
    fn formatted_message(&self) -> String;

    /// Check if this entry matches a minimum log level
    fn matches_level(&self, min_level: LogLevel) -> bool;
}

impl LogEntryExt for LogEntry {
    fn level(&self) -> LogLevel {
        self.message.level
    }

    fn formatted_message(&self) -> String {
        self.message.formatted()
    }

    fn matches_level(&self, min_level: LogLevel) -> bool {
        self.message.level >= min_level
    }
}

/// Log container data structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogContainer {
    /// Name of this log
    pub name: String,
    /// Log destination type
    pub destination: LogDestination,
    /// Minimum log level to accept
    pub min_level: LogLevel,
    /// Maximum number of entries to keep (0 = unlimited)
    pub max_entries: usize,
    /// Current log entries
    pub entries: Vec<LogEntry>,
    /// Whether this log is currently active
    pub active: bool,
}

impl Hash for LogContainer {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.name.hash(state);
        self.destination.hash(state);
    }
}

impl LogContainer {
    /// Create a new log container
    pub fn new(name: String, destination: LogDestination, min_level: LogLevel) -> Self {
        Self {
            name,
            destination,
            min_level,
            max_entries: 0, // Unlimited by default
            entries: Vec::new(),
            active: true,
        }
    }

    /// Create with maximum entries limit
    pub fn with_max_entries(mut self, max_entries: usize) -> Self {
        self.max_entries = max_entries;
        self
    }

    /// Add a log entry if it meets the minimum level requirement
    pub fn add_entry(&mut self, entry: LogEntry) -> bool {
        if !self.active || !entry.matches_level(self.min_level) {
            return false;
        }

        self.entries.push(entry);

        // Enforce max entries limit
        if self.max_entries > 0 && self.entries.len() > self.max_entries {
            self.entries.remove(0); // Remove oldest entry
        }

        true
    }

    /// Get entries matching a minimum level
    pub fn get_entries(&self, min_level: Option<LogLevel>) -> Vec<&LogEntry> {
        match min_level {
            Some(level) => self
                .entries
                .iter()
                .filter(|e| e.matches_level(level))
                .collect(),
            None => self.entries.iter().collect(),
        }
    }

    /// Get entries within a time range
    pub fn get_entries_since(&self, since: DateTime<Utc>) -> Vec<&LogEntry> {
        self.entries
            .iter()
            .filter(|e| e.timestamp >= since)
            .collect()
    }

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

    /// Get entry count
    pub fn entry_count(&self) -> usize {
        self.entries.len()
    }

    /// Enable/disable this log
    pub fn set_active(&mut self, active: bool) {
        self.active = active;
    }

    /// Check if log is active
    pub fn is_active(&self) -> bool {
        self.active
    }

    /// Set minimum log level
    pub fn set_min_level(&mut self, level: LogLevel) {
        self.min_level = level;
    }
}

/// Type alias for Log as a Node containing LogContainer
pub type Log = Node<LogContainer>;

/// Extension trait for Log to add log-specific functionality
pub trait LogExt {
    /// Create a new log node
    fn new_log(name: String, destination: LogDestination, min_level: LogLevel) -> Self;

    /// Add an entry to this log
    fn add_entry(&mut self, entry: LogEntry) -> bool;

    /// Get the log destination
    fn destination(&self) -> &LogDestination;

    /// Get the minimum log level
    fn min_level(&self) -> LogLevel;
}

impl LogExt for Log {
    fn new_log(name: String, destination: LogDestination, min_level: LogLevel) -> Self {
        let container = LogContainer::new(name.clone(), destination, min_level);
        Node::new(container, Some(name))
    }

    fn add_entry(&mut self, entry: LogEntry) -> bool {
        let result = self.node.add_entry(entry);
        if result {
            self.modified = Utc::now();
        }
        result
    }

    fn destination(&self) -> &LogDestination {
        &self.node.destination
    }

    fn min_level(&self) -> LogLevel {
        self.node.min_level
    }
}

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

    #[test]
    fn test_log_level_conversion() {
        assert_eq!(LogLevel::parse_str("INFO"), Some(LogLevel::Info));
        assert_eq!(LogLevel::parse_str("error"), Some(LogLevel::Error));
        assert_eq!(LogLevel::parse_str("INVALID"), None);

        assert_eq!(LogLevel::Info.as_str(), "INFO");
        assert_eq!(LogLevel::Error.to_priority(), MessagePriority::Critical);
    }

    #[test]
    fn test_log_message_creation() {
        let msg = LogMessage::new(LogLevel::Info, "Test message".to_string())
            .with_module("test_module".to_string())
            .with_function("test_function".to_string());

        assert_eq!(msg.level, LogLevel::Info);
        assert_eq!(msg.message, "Test message");
        assert_eq!(msg.module, Some("test_module".to_string()));
        assert_eq!(msg.function, Some("test_function".to_string()));
    }

    #[test]
    fn test_log_entry_creation() {
        let source = Location::service("test-service");
        let entry = LogEntryBuilder::new_entry(
            source,
            LogDestination::System,
            LogLevel::Info,
            "Test log entry".to_string(),
        );

        assert_eq!(entry.level(), LogLevel::Info);
        assert_eq!(entry.message.message, "Test log entry");
        assert_eq!(entry.destination, LogDestination::System.to_location());
    }

    #[test]
    fn test_log_container() {
        let mut container = LogContainer::new(
            "test-log".to_string(),
            LogDestination::System,
            LogLevel::Info,
        );

        let entry = LogEntryBuilder::new_entry(
            Location::service("test"),
            LogDestination::System,
            LogLevel::Info,
            "Test message".to_string(),
        );

        assert!(container.add_entry(entry));
        assert_eq!(container.entry_count(), 1);

        // Test level filtering
        let debug_entry = LogEntryBuilder::new_entry(
            Location::service("test"),
            LogDestination::System,
            LogLevel::Debug,
            "Debug message".to_string(),
        );

        assert!(!container.add_entry(debug_entry)); // Should be filtered out
        assert_eq!(container.entry_count(), 1);
    }

    #[test]
    fn test_log_node() {
        let mut log = Log::new_log(
            "system-log".to_string(),
            LogDestination::System,
            LogLevel::Info,
        );

        let entry = LogEntryBuilder::new_entry(
            Location::service("test"),
            LogDestination::System,
            LogLevel::Warn,
            "Warning message".to_string(),
        );

        let initial_modified = log.modified;
        assert!(log.add_entry(entry));
        assert!(log.modified > initial_modified);
        assert_eq!(log.node.entry_count(), 1);
    }
}