code-mesh-core 0.1.0

High-performance, WASM-powered distributed swarm intelligence core library for concurrent code execution and neural mesh computing
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
//! Configuration management for Code Mesh Core
//!
//! This module provides a comprehensive configuration system that supports:
//! - Multiple configuration sources (files, environment, programmatic)
//! - Provider-specific configurations
//! - Tool configurations and permissions
//! - Runtime feature detection
//! - Configuration validation and defaults

use crate::{Error, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;

/// Main configuration structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    /// General application settings
    pub app: AppConfig,
    
    /// Provider configurations
    pub providers: HashMap<String, ProviderConfig>,
    
    /// Tool configurations
    pub tools: ToolConfig,
    
    /// Storage configuration
    pub storage: StorageConfig,
    
    /// Authentication configuration
    pub auth: AuthConfig,
    
    /// Session configuration
    pub session: SessionConfig,
    
    /// Memory configuration
    pub memory: MemoryConfig,
    
    /// Agent configuration
    pub agent: AgentConfig,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            app: AppConfig::default(),
            providers: HashMap::new(),
            tools: ToolConfig::default(),
            storage: StorageConfig::default(),
            auth: AuthConfig::default(),
            session: SessionConfig::default(),
            memory: MemoryConfig::default(),
            agent: AgentConfig::default(),
        }
    }
}

/// Application-level configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppConfig {
    /// Application name
    pub name: String,
    
    /// Version information
    pub version: String,
    
    /// Data directory path
    pub data_dir: Option<PathBuf>,
    
    /// Configuration directory path
    pub config_dir: Option<PathBuf>,
    
    /// Log level
    pub log_level: String,
    
    /// Feature flags
    pub features: FeatureConfig,
}

impl Default for AppConfig {
    fn default() -> Self {
        Self {
            name: "code-mesh".to_string(),
            version: crate::VERSION.to_string(),
            data_dir: None,
            config_dir: None,
            log_level: "info".to_string(),
            features: FeatureConfig::default(),
        }
    }
}

/// Feature configuration flags
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeatureConfig {
    /// Enable compression for storage
    pub compression: bool,
    
    /// Enable file watching
    pub file_watching: bool,
    
    /// Enable advanced cryptography
    pub advanced_crypto: bool,
    
    /// Enable telemetry
    pub telemetry: bool,
    
    /// Enable experimental features
    pub experimental: bool,
}

impl Default for FeatureConfig {
    fn default() -> Self {
        Self {
            compression: crate::features::HAS_COMPRESSION,
            file_watching: crate::features::HAS_FILE_WATCHING,
            advanced_crypto: crate::features::HAS_ADVANCED_CRYPTO,
            telemetry: false,
            experimental: false,
        }
    }
}

/// Provider configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderConfig {
    /// Provider ID
    pub id: String,
    
    /// Provider name
    pub name: String,
    
    /// Base URL for API
    pub base_url: Option<String>,
    
    /// API version
    pub api_version: Option<String>,
    
    /// Default model to use
    pub default_model: Option<String>,
    
    /// Model configurations
    pub models: HashMap<String, ModelConfig>,
    
    /// Provider-specific options
    pub options: HashMap<String, serde_json::Value>,
    
    /// Rate limiting configuration
    pub rate_limit: RateLimitConfig,
    
    /// Timeout configuration
    pub timeout: TimeoutConfig,
    
    /// Retry configuration
    pub retry: RetryConfig,
    
    /// Whether this provider is enabled
    pub enabled: bool,
}

impl Default for ProviderConfig {
    fn default() -> Self {
        Self {
            id: String::new(),
            name: String::new(),
            base_url: None,
            api_version: None,
            default_model: None,
            models: HashMap::new(),
            options: HashMap::new(),
            rate_limit: RateLimitConfig::default(),
            timeout: TimeoutConfig::default(),
            retry: RetryConfig::default(),
            enabled: true,
        }
    }
}

/// Model configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelConfig {
    /// Model ID
    pub id: String,
    
    /// Model name
    pub name: String,
    
    /// Maximum context length
    pub max_context: Option<u32>,
    
    /// Maximum output tokens
    pub max_output: Option<u32>,
    
    /// Default temperature
    pub temperature: Option<f32>,
    
    /// Supports tool calling
    pub supports_tools: bool,
    
    /// Supports vision/images
    pub supports_vision: bool,
    
    /// Supports streaming
    pub supports_streaming: bool,
    
    /// Supports caching
    pub supports_caching: bool,
    
    /// Cost information
    pub cost: CostConfig,
    
    /// Model-specific options
    pub options: HashMap<String, serde_json::Value>,
}

impl Default for ModelConfig {
    fn default() -> Self {
        Self {
            id: String::new(),
            name: String::new(),
            max_context: None,
            max_output: None,
            temperature: None,
            supports_tools: true,
            supports_vision: false,
            supports_streaming: true,
            supports_caching: false,
            cost: CostConfig::default(),
            options: HashMap::new(),
        }
    }
}

/// Cost configuration for models
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CostConfig {
    /// Input tokens cost per 1K tokens
    pub input_cost_per_1k: f64,
    
    /// Output tokens cost per 1K tokens
    pub output_cost_per_1k: f64,
    
    /// Cache read cost per 1K tokens
    pub cache_read_cost_per_1k: Option<f64>,
    
    /// Cache write cost per 1K tokens
    pub cache_write_cost_per_1k: Option<f64>,
    
    /// Currency for costs
    pub currency: String,
}

impl Default for CostConfig {
    fn default() -> Self {
        Self {
            input_cost_per_1k: 0.0,
            output_cost_per_1k: 0.0,
            cache_read_cost_per_1k: None,
            cache_write_cost_per_1k: None,
            currency: "USD".to_string(),
        }
    }
}

/// Rate limiting configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RateLimitConfig {
    /// Requests per minute
    pub requests_per_minute: Option<u32>,
    
    /// Tokens per minute
    pub tokens_per_minute: Option<u32>,
    
    /// Concurrent requests
    pub concurrent_requests: Option<u32>,
}

impl Default for RateLimitConfig {
    fn default() -> Self {
        Self {
            requests_per_minute: None,
            tokens_per_minute: None,
            concurrent_requests: Some(4),
        }
    }
}

/// Timeout configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeoutConfig {
    /// Connect timeout in seconds
    pub connect: u64,
    
    /// Request timeout in seconds
    pub request: u64,
    
    /// Stream timeout in seconds
    pub stream: u64,
}

impl Default for TimeoutConfig {
    fn default() -> Self {
        Self {
            connect: 10,
            request: 300,
            stream: 600,
        }
    }
}

/// Retry configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryConfig {
    /// Maximum number of retries
    pub max_retries: u32,
    
    /// Base delay in milliseconds
    pub base_delay: u64,
    
    /// Maximum delay in milliseconds
    pub max_delay: u64,
    
    /// Exponential backoff multiplier
    pub backoff_multiplier: f64,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: 3,
            base_delay: 1000,
            max_delay: 60000,
            backoff_multiplier: 2.0,
        }
    }
}

/// Tool system configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolConfig {
    /// Enabled tools
    pub enabled: Vec<String>,
    
    /// Disabled tools
    pub disabled: Vec<String>,
    
    /// Tool-specific configurations
    pub tool_configs: HashMap<String, ToolSpecificConfig>,
    
    /// Default permission level
    pub default_permission: String,
    
    /// Sandbox configuration
    pub sandbox: SandboxConfig,
}

impl Default for ToolConfig {
    fn default() -> Self {
        Self {
            enabled: vec!["read".to_string(), "write".to_string(), "edit".to_string()],
            disabled: Vec::new(),
            tool_configs: HashMap::new(),
            default_permission: "restricted".to_string(),
            sandbox: SandboxConfig::default(),
        }
    }
}

/// Tool-specific configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolSpecificConfig {
    /// Permission level for this tool
    pub permission: String,
    
    /// Allowed file patterns
    pub allowed_patterns: Vec<String>,
    
    /// Denied file patterns
    pub denied_patterns: Vec<String>,
    
    /// Maximum file size in bytes
    pub max_file_size: Option<u64>,
    
    /// Tool-specific options
    pub options: HashMap<String, serde_json::Value>,
}

impl Default for ToolSpecificConfig {
    fn default() -> Self {
        Self {
            permission: "restricted".to_string(),
            allowed_patterns: Vec::new(),
            denied_patterns: Vec::new(),
            max_file_size: None,
            options: HashMap::new(),
        }
    }
}

/// Sandbox configuration for tool execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SandboxConfig {
    /// Enable sandboxing
    pub enabled: bool,
    
    /// Allowed directories
    pub allowed_dirs: Vec<PathBuf>,
    
    /// Denied directories
    pub denied_dirs: Vec<PathBuf>,
    
    /// Network access allowed
    pub network_access: bool,
    
    /// Maximum execution time in seconds
    pub max_execution_time: u64,
    
    /// Maximum memory usage in MB
    pub max_memory_mb: Option<u64>,
}

impl Default for SandboxConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            allowed_dirs: Vec::new(),
            denied_dirs: Vec::new(),
            network_access: false,
            max_execution_time: 30,
            max_memory_mb: Some(512),
        }
    }
}

/// Storage configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageConfig {
    /// Storage backend type
    pub backend: String,
    
    /// Connection string or path
    pub connection: String,
    
    /// Enable compression
    pub compression: bool,
    
    /// Encryption configuration
    pub encryption: EncryptionConfig,
    
    /// Backup configuration
    pub backup: BackupConfig,
}

impl Default for StorageConfig {
    fn default() -> Self {
        Self {
            backend: "file".to_string(),
            connection: "data/storage".to_string(),
            compression: crate::features::HAS_COMPRESSION,
            encryption: EncryptionConfig::default(),
            backup: BackupConfig::default(),
        }
    }
}

/// Encryption configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptionConfig {
    /// Enable encryption
    pub enabled: bool,
    
    /// Encryption algorithm
    pub algorithm: String,
    
    /// Key derivation method
    pub key_derivation: String,
}

impl Default for EncryptionConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            algorithm: "aes-256-gcm".to_string(),
            key_derivation: "pbkdf2".to_string(),
        }
    }
}

/// Backup configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupConfig {
    /// Enable automatic backups
    pub enabled: bool,
    
    /// Backup interval in hours
    pub interval_hours: u64,
    
    /// Maximum number of backups to keep
    pub max_backups: u32,
    
    /// Backup directory
    pub backup_dir: Option<PathBuf>,
}

impl Default for BackupConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            interval_hours: 24,
            max_backups: 7,
            backup_dir: None,
        }
    }
}

/// Authentication configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthConfig {
    /// Storage backend for auth data
    pub storage_backend: String,
    
    /// Token refresh interval in minutes
    pub refresh_interval: u64,
    
    /// Security configuration
    pub security: SecurityConfig,
}

impl Default for AuthConfig {
    fn default() -> Self {
        Self {
            storage_backend: "encrypted_file".to_string(),
            refresh_interval: 60,
            security: SecurityConfig::default(),
        }
    }
}

/// Security configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityConfig {
    /// Enable key rotation
    pub key_rotation: bool,
    
    /// Key rotation interval in days
    pub rotation_interval_days: u64,
    
    /// Enable audit logging
    pub audit_logging: bool,
}

impl Default for SecurityConfig {
    fn default() -> Self {
        Self {
            key_rotation: true,
            rotation_interval_days: 30,
            audit_logging: true,
        }
    }
}

/// Session configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionConfig {
    /// Maximum number of messages per session
    pub max_messages: u32,
    
    /// Maximum session age in hours
    pub max_age_hours: u64,
    
    /// Auto-save interval in seconds
    pub auto_save_interval: u64,
    
    /// Context window configuration
    pub context_window: ContextWindowConfig,
}

impl Default for SessionConfig {
    fn default() -> Self {
        Self {
            max_messages: 1000,
            max_age_hours: 24 * 7, // 1 week
            auto_save_interval: 30,
            context_window: ContextWindowConfig::default(),
        }
    }
}

/// Context window configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextWindowConfig {
    /// Maximum tokens in context
    pub max_tokens: u32,
    
    /// Context trimming strategy
    pub trim_strategy: String,
    
    /// Preserve system messages
    pub preserve_system: bool,
    
    /// Preserve recent messages count
    pub preserve_recent: u32,
}

impl Default for ContextWindowConfig {
    fn default() -> Self {
        Self {
            max_tokens: 100000,
            trim_strategy: "preserve_recent".to_string(),
            preserve_system: true,
            preserve_recent: 10,
        }
    }
}

/// Memory configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryConfig {
    /// Memory backend type
    pub backend: String,
    
    /// Maximum memory entries
    pub max_entries: u32,
    
    /// Memory TTL in hours
    pub ttl_hours: u64,
    
    /// Enable memory compression
    pub compression: bool,
    
    /// Memory indexing configuration
    pub indexing: IndexingConfig,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            backend: "hybrid".to_string(),
            max_entries: 10000,
            ttl_hours: 24 * 30, // 30 days
            compression: crate::features::HAS_COMPRESSION,
            indexing: IndexingConfig::default(),
        }
    }
}

/// Memory indexing configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexingConfig {
    /// Enable semantic search
    pub semantic_search: bool,
    
    /// Enable full-text search
    pub full_text_search: bool,
    
    /// Embedding model for semantic search
    pub embedding_model: Option<String>,
}

impl Default for IndexingConfig {
    fn default() -> Self {
        Self {
            semantic_search: false,
            full_text_search: true,
            embedding_model: None,
        }
    }
}

/// Agent configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentConfig {
    /// Maximum concurrent agents
    pub max_concurrent: u32,
    
    /// Agent timeout in seconds
    pub timeout_seconds: u64,
    
    /// Enable agent collaboration
    pub collaboration: bool,
    
    /// Default agent capabilities
    pub default_capabilities: Vec<String>,
}

impl Default for AgentConfig {
    fn default() -> Self {
        Self {
            max_concurrent: 4,
            timeout_seconds: 300,
            collaboration: true,
            default_capabilities: vec![
                "read".to_string(),
                "write".to_string(),
                "execute".to_string(),
            ],
        }
    }
}

/// Configuration manager for loading and managing configurations
pub struct ConfigManager {
    config: Config,
    config_path: Option<PathBuf>,
}

impl ConfigManager {
    /// Create a new configuration manager
    pub fn new() -> Self {
        Self {
            config: Config::default(),
            config_path: None,
        }
    }

    /// Load configuration from file
    pub async fn load_from_file<P: Into<PathBuf>>(&mut self, path: P) -> Result<()> {
        let path = path.into();
        let content = std::fs::read_to_string(&path)
            .map_err(|e| Error::Io(e))?;
        
        let config: Config = match path.extension().and_then(|s| s.to_str()) {
            Some("json") => serde_json::from_str(&content)?,
            Some("toml") => toml::from_str(&content)
                .map_err(|e| Error::Other(anyhow::anyhow!("TOML parse error: {}", e)))?,
            Some("yaml") | Some("yml") => {
                return Err(Error::Other(anyhow::anyhow!("YAML support not implemented")));
            }
            _ => return Err(Error::Other(anyhow::anyhow!("Unsupported config file format"))),
        };

        self.config = config;
        self.config_path = Some(path);
        Ok(())
    }

    /// Load configuration from environment variables
    pub fn load_from_env(&mut self) -> Result<()> {
        // Load environment variables with CODE_MESH_ prefix
        let mut config = self.config.clone();
        
        if let Ok(log_level) = std::env::var("CODE_MESH_LOG_LEVEL") {
            config.app.log_level = log_level;
        }
        
        if let Ok(data_dir) = std::env::var("CODE_MESH_DATA_DIR") {
            config.app.data_dir = Some(PathBuf::from(data_dir));
        }
        
        // Add more environment variable mappings as needed
        
        self.config = config;
        Ok(())
    }

    /// Save configuration to file
    pub async fn save_to_file<P: Into<PathBuf>>(&self, path: P) -> Result<()> {
        let path = path.into();
        
        let content = match path.extension().and_then(|s| s.to_str()) {
            Some("json") => serde_json::to_string_pretty(&self.config)?,
            Some("toml") => toml::to_string_pretty(&self.config)
                .map_err(|e| Error::Other(anyhow::anyhow!("TOML serialize error: {}", e)))?,
            _ => return Err(Error::Other(anyhow::anyhow!("Unsupported config file format"))),
        };
        
        std::fs::write(path, content)?;
        Ok(())
    }

    /// Get the current configuration
    pub fn config(&self) -> &Config {
        &self.config
    }

    /// Get mutable access to the configuration
    pub fn config_mut(&mut self) -> &mut Config {
        &mut self.config
    }

    /// Validate the current configuration
    pub fn validate(&self) -> Result<()> {
        // Validate provider configurations
        for (id, provider) in &self.config.providers {
            if provider.id != *id {
                return Err(Error::Other(anyhow::anyhow!(
                    "Provider ID mismatch: {} != {}",
                    provider.id,
                    id
                )));
            }
        }

        // Validate tool configurations
        if self.config.tools.default_permission.is_empty() {
            return Err(Error::Other(anyhow::anyhow!(
                "Default permission cannot be empty"
            )));
        }

        // Add more validation rules as needed

        Ok(())
    }

    /// Get configuration for a specific provider
    pub fn get_provider_config(&self, provider_id: &str) -> Option<&ProviderConfig> {
        self.config.providers.get(provider_id)
    }

    /// Get configuration for a specific tool
    pub fn get_tool_config(&self, tool_id: &str) -> Option<&ToolSpecificConfig> {
        self.config.tools.tool_configs.get(tool_id)
    }

    /// Merge another configuration into the current one
    pub fn merge(&mut self, other: Config) -> Result<()> {
        // Deep merge configurations
        // This is a simplified version - in practice, you'd want more sophisticated merging
        for (id, provider) in other.providers {
            self.config.providers.insert(id, provider);
        }

        for (id, tool_config) in other.tools.tool_configs {
            self.config.tools.tool_configs.insert(id, tool_config);
        }

        Ok(())
    }
}

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