universal-bot-core 1.0.0

Core functionality for Universal Bot AI automation framework with AWS Bedrock
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
//! Configuration management for Universal Bot
//!
//! This module provides configuration structures and builders for the bot,
//! following the builder pattern for ergonomic configuration.

use std::time::Duration;

use anyhow::{Context as _, Result};
use serde::{Deserialize, Serialize};
use validator::{Validate, ValidationError};

use crate::error::Error;

/// Main bot configuration
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct BotConfig {
    /// AI model to use for generation
    #[validate(custom(function = "validate_model"))]
    pub model: String,

    /// Temperature for generation (0.0 to 1.0)
    #[validate(range(min = 0.0, max = 1.0))]
    pub temperature: f32,

    /// Maximum tokens to generate
    #[validate(range(min = 1, max = 100_000))]
    pub max_tokens: usize,

    /// Request timeout
    #[serde(with = "humantime_serde")]
    pub timeout: Duration,

    /// Number of retries for failed requests
    #[validate(range(min = 0, max = 10))]
    pub max_retries: u32,

    /// Enable request logging
    pub enable_logging: bool,

    /// Enable cost tracking
    pub enable_cost_tracking: bool,

    /// Context configuration
    pub context_config: ContextConfig,

    /// Pipeline configuration
    pub pipeline_config: PipelineConfig,

    /// Plugin configuration
    pub plugin_config: PluginConfig,
}

impl BotConfig {
    /// Create a new configuration builder
    #[must_use]
    pub fn builder() -> BotConfigBuilder {
        BotConfigBuilder::default()
    }

    /// Validate the configuration
    ///
    /// # Errors
    ///
    /// Returns an error if validation fails.
    pub fn validate(&self) -> Result<()> {
        Validate::validate(self).map_err(|e| Error::Validation(e.to_string()))?;
        Ok(())
    }

    /// Load configuration from environment variables
    ///
    /// # Errors
    ///
    /// Returns an error if required environment variables are missing.
    pub fn from_env() -> Result<Self> {
        let model = std::env::var("DEFAULT_MODEL")
            .unwrap_or_else(|_| "anthropic.claude-opus-4-1".to_string());

        let temperature = std::env::var("TEMPERATURE")
            .unwrap_or_else(|_| "0.1".to_string())
            .parse()
            .context("Invalid TEMPERATURE value")?;

        let max_tokens = std::env::var("MAX_TOKENS")
            .unwrap_or_else(|_| "2048".to_string())
            .parse()
            .context("Invalid MAX_TOKENS value")?;

        Ok(Self {
            model,
            temperature,
            max_tokens,
            ..Default::default()
        })
    }
}

impl Default for BotConfig {
    fn default() -> Self {
        Self {
            model: "anthropic.claude-opus-4-1".to_string(),
            temperature: 0.1,
            max_tokens: 2048,
            timeout: Duration::from_secs(30),
            max_retries: 3,
            enable_logging: true,
            enable_cost_tracking: true,
            context_config: ContextConfig::default(),
            pipeline_config: PipelineConfig::default(),
            plugin_config: PluginConfig::default(),
        }
    }
}

/// Configuration for context management
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct ContextConfig {
    /// Maximum context size in tokens
    #[validate(range(min = 100, max = 100_000))]
    pub max_context_tokens: usize,

    /// Context TTL
    #[serde(with = "humantime_serde")]
    pub context_ttl: Duration,

    /// Enable context persistence
    pub persist_context: bool,

    /// Context storage backend
    pub storage_backend: StorageBackend,
}

impl Default for ContextConfig {
    fn default() -> Self {
        Self {
            max_context_tokens: 4096,
            context_ttl: Duration::from_secs(3600),
            persist_context: false,
            storage_backend: StorageBackend::Memory,
        }
    }
}

/// Storage backend for context persistence
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum StorageBackend {
    /// In-memory storage (default)
    Memory,
    /// Redis storage
    Redis {
        /// Redis connection URL
        url: String,
    },
    /// `PostgreSQL` storage
    Postgres {
        /// `PostgreSQL` connection URL
        url: String,
    },
    /// `SQLite` storage
    Sqlite {
        /// `SQLite` database file path
        path: String,
    },
}

/// Configuration for the message pipeline
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct PipelineConfig {
    /// Enable message sanitization
    pub enable_sanitization: bool,

    /// Enable message enrichment
    pub enable_enrichment: bool,

    /// Maximum pipeline processing time
    #[serde(with = "humantime_serde")]
    pub max_processing_time: Duration,

    /// Pipeline stages to enable
    pub enabled_stages: Vec<String>,
}

impl Default for PipelineConfig {
    fn default() -> Self {
        Self {
            enable_sanitization: true,
            enable_enrichment: true,
            max_processing_time: Duration::from_secs(10),
            enabled_stages: vec![
                "sanitize".to_string(),
                "enrich".to_string(),
                "route".to_string(),
                "process".to_string(),
                "format".to_string(),
            ],
        }
    }
}

/// Configuration for plugins
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginConfig {
    /// Enable plugin system
    pub enable_plugins: bool,

    /// Plugin directories to scan
    pub plugin_dirs: Vec<String>,

    /// Plugins to auto-load
    pub auto_load: Vec<String>,

    /// Plugin timeout
    #[serde(with = "humantime_serde")]
    pub plugin_timeout: Duration,
}

impl Default for PluginConfig {
    fn default() -> Self {
        Self {
            enable_plugins: true,
            plugin_dirs: vec!["plugins".to_string()],
            auto_load: Vec::new(),
            plugin_timeout: Duration::from_secs(5),
        }
    }
}

/// Builder for `BotConfig`
#[derive(Default)]
pub struct BotConfigBuilder {
    model: Option<String>,
    temperature: Option<f32>,
    max_tokens: Option<usize>,
    timeout: Option<Duration>,
    max_retries: Option<u32>,
    enable_logging: Option<bool>,
    enable_cost_tracking: Option<bool>,
    context_config: Option<ContextConfig>,
    pipeline_config: Option<PipelineConfig>,
    plugin_config: Option<PluginConfig>,
}

impl BotConfigBuilder {
    /// Set the AI model
    #[must_use]
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    /// Set the temperature
    #[must_use]
    pub fn temperature(mut self, temperature: f32) -> Self {
        self.temperature = Some(temperature);
        self
    }

    /// Set the maximum tokens
    #[must_use]
    pub fn max_tokens(mut self, max_tokens: usize) -> Self {
        self.max_tokens = Some(max_tokens);
        self
    }

    /// Set the timeout
    #[must_use]
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Set the maximum retries
    #[must_use]
    pub fn max_retries(mut self, max_retries: u32) -> Self {
        self.max_retries = Some(max_retries);
        self
    }

    /// Enable or disable logging
    #[must_use]
    pub fn enable_logging(mut self, enable: bool) -> Self {
        self.enable_logging = Some(enable);
        self
    }

    /// Enable or disable cost tracking
    #[must_use]
    pub fn enable_cost_tracking(mut self, enable: bool) -> Self {
        self.enable_cost_tracking = Some(enable);
        self
    }

    /// Set the context configuration
    #[must_use]
    pub fn context_config(mut self, config: ContextConfig) -> Self {
        self.context_config = Some(config);
        self
    }

    /// Set the pipeline configuration
    #[must_use]
    pub fn pipeline_config(mut self, config: PipelineConfig) -> Self {
        self.pipeline_config = Some(config);
        self
    }

    /// Set the plugin configuration
    #[must_use]
    pub fn plugin_config(mut self, config: PluginConfig) -> Self {
        self.plugin_config = Some(config);
        self
    }

    /// Build the configuration
    ///
    /// # Errors
    ///
    /// Returns an error if required fields are missing or validation fails.
    pub fn build(self) -> Result<BotConfig> {
        let config = BotConfig {
            model: self.model.context("model is required")?,
            temperature: self.temperature.unwrap_or(0.1),
            max_tokens: self.max_tokens.unwrap_or(2048),
            timeout: self.timeout.unwrap_or(Duration::from_secs(30)),
            max_retries: self.max_retries.unwrap_or(3),
            enable_logging: self.enable_logging.unwrap_or(true),
            enable_cost_tracking: self.enable_cost_tracking.unwrap_or(true),
            context_config: self.context_config.unwrap_or_default(),
            pipeline_config: self.pipeline_config.unwrap_or_default(),
            plugin_config: self.plugin_config.unwrap_or_default(),
        };

        config.validate()?;
        Ok(config)
    }
}

/// Validate model name
fn validate_model(model: &str) -> Result<(), ValidationError> {
    const ALLOWED_MODELS: &[&str] = &[
        "anthropic.claude-opus-4-1",
        "us.anthropic.claude-opus-4-1-20250805-v1:0", // Opus 4.1 inference profile
        "anthropic.claude-sonnet-4",
        "anthropic.claude-haiku",
        "meta.llama3-70b-instruct",
        "meta.llama3-8b-instruct",
        "amazon.titan-text-express",
        "ai21.j2-ultra",
        "ai21.j2-mid",
    ];

    if ALLOWED_MODELS.contains(&model) {
        Ok(())
    } else {
        Err(ValidationError::new("invalid_model"))
    }
}

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

    #[test]
    fn test_default_config() {
        let config = BotConfig::default();
        assert_eq!(config.model, "anthropic.claude-opus-4-1");
        assert!((config.temperature - 0.1).abs() < f32::EPSILON);
        assert_eq!(config.max_tokens, 2048);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_config_builder() {
        let config = BotConfig::builder()
            .model("anthropic.claude-sonnet-4")
            .temperature(0.5)
            .max_tokens(4096)
            .timeout(Duration::from_secs(60))
            .build();

        assert!(config.is_ok());
        let config = config.unwrap();
        assert_eq!(config.model, "anthropic.claude-sonnet-4");
        assert!((config.temperature - 0.5).abs() < f32::EPSILON);
        assert_eq!(config.max_tokens, 4096);
    }

    #[test]
    fn test_invalid_model() {
        let config = BotConfig::builder().model("invalid-model").build();

        assert!(config.is_err());
    }

    #[test]
    fn test_invalid_temperature() {
        let config = BotConfig {
            temperature: 1.5,
            ..Default::default()
        };
        assert!(config.validate().is_err());

        let config = BotConfig {
            temperature: -0.1,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_from_env() {
        std::env::set_var("DEFAULT_MODEL", "anthropic.claude-opus-4-1");
        std::env::set_var("TEMPERATURE", "0.7");
        std::env::set_var("MAX_TOKENS", "4096");

        let config = BotConfig::from_env();
        assert!(config.is_ok());

        let config = config.unwrap();
        assert!((config.temperature - 0.7).abs() < f32::EPSILON);
        assert_eq!(config.max_tokens, 4096);
    }

    #[cfg(feature = "property-testing")]
    mod property_tests {
        use super::*;
        use proptest::prelude::*;

        proptest! {
            #[test]
            fn test_temperature_validation(temp in -10.0f32..10.0) {
                let config = BotConfig {
                    temperature: temp,
                    ..Default::default()
                };

                let result = config.validate();
                if (0.0..=1.0).contains(&temp) {
                    prop_assert!(result.is_ok());
                } else {
                    prop_assert!(result.is_err());
                }
            }

            #[test]
            fn test_max_tokens_validation(tokens in 0usize..200_000) {
                let config = BotConfig {
                    max_tokens: tokens,
                    ..Default::default()
                };

                let result = config.validate();
                if tokens > 0 && tokens <= 100_000 {
                    prop_assert!(result.is_ok());
                } else {
                    prop_assert!(result.is_err());
                }
            }
        }
    }
}