synapse 1.1.0

Neural Communication Network with Federated Identity and Blockchain Trust
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
//! Configuration management for EMRP

use crate::types::{EmailConfig, SmtpConfig, ImapConfig};
use crate::error::{ConfigError, Result};
use serde::{Deserialize, Serialize};
#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;

/// Main configuration for EMRP
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    /// Entity configuration
    pub entity: EntityConfig,
    /// Email configuration
    pub email: EmailConfig,
    /// Router configuration
    pub router: RouterConfig,
    /// Security configuration
    pub security: SecurityConfig,
    /// Logging configuration
    pub logging: LoggingConfig,
}

/// Entity-specific configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityConfig {
    /// Local entity name
    pub local_name: String,
    /// Entity type
    pub entity_type: String,
    /// Domain for generating global ID
    pub domain: String,
    /// Capabilities this entity provides
    pub capabilities: Vec<String>,
    /// Display name
    pub display_name: Option<String>,
}

/// Router configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RouterConfig {
    /// Maximum concurrent connections
    pub max_connections: usize,
    /// Message queue size
    pub queue_size: usize,
    /// Connection timeout in seconds
    pub connection_timeout: u64,
    /// Retry attempts for failed messages
    pub max_retries: u32,
    /// Enable real-time optimizations
    pub enable_realtime: bool,
    /// IMAP IDLE timeout in seconds
    pub idle_timeout: u64,
}

/// Security configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityConfig {
    /// Path to private key file
    pub private_key_path: Option<String>,
    /// Path to public key file
    pub public_key_path: Option<String>,
    /// Auto-generate keys if not found
    pub auto_generate_keys: bool,
    /// Default security level
    pub default_security_level: String,
    /// Trusted domains
    pub trusted_domains: Vec<String>,
    /// Require encryption for these entity types
    pub require_encryption_for: Vec<String>,
}

/// Logging configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggingConfig {
    /// Log level (trace, debug, info, warn, error)
    pub level: String,
    /// Log format (compact, pretty, json)
    pub format: String,
    /// Log to file
    pub file: Option<String>,
    /// Log message content (be careful with privacy)
    pub log_message_content: bool,
}

impl Config {
    /// Load configuration from a TOML file (not available on WASM)
    #[cfg(not(target_arch = "wasm32"))]
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let content = std::fs::read_to_string(path.as_ref())
            .map_err(|e| ConfigError::FileNotFound(e.to_string()))?;

        toml::from_str(&content)
            .map_err(|e| ConfigError::InvalidFormat(e.to_string()).into())
    }

    /// Save configuration to a TOML file (not available on WASM)
    #[cfg(not(target_arch = "wasm32"))]
    pub fn to_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let content = toml::to_string_pretty(self)
            .map_err(|e| ConfigError::InvalidFormat(e.to_string()))?;

        std::fs::write(path.as_ref(), content)
            .map_err(|e| ConfigError::FileNotFound(e.to_string()).into())
    }

    /// Create a default configuration
    pub fn default_for_entity(local_name: impl Into<String>, entity_type: impl Into<String>) -> Self {
        let local_name = local_name.into();
        let entity_type = entity_type.into();
        
        Self {
            entity: EntityConfig {
                local_name: local_name.clone(),
                entity_type: entity_type.clone(),
                domain: "synapse.local".to_string(),
                capabilities: Self::default_capabilities_for_type(&entity_type),
                display_name: Some(format!("{} ({})", local_name, entity_type)),
            },
            email: EmailConfig {
                smtp: SmtpConfig {
                    host: "localhost".to_string(),
                    port: 587,
                    username: format!("{}@synapse.local", local_name.to_lowercase()),
                    password: "changeme".to_string(),
                    use_tls: true,
                    use_ssl: false,
                },
                imap: ImapConfig {
                    host: "localhost".to_string(),
                    port: 993,
                    username: format!("{}@synapse.local", local_name.to_lowercase()),
                    password: "changeme".to_string(),
                    use_ssl: true,
                },
            },
            router: RouterConfig {
                max_connections: 100,
                queue_size: 1000,
                connection_timeout: 30,
                max_retries: 3,
                enable_realtime: true,
                idle_timeout: 300,
            },
            security: SecurityConfig {
                private_key_path: Some(format!("{}_private.pem", local_name.to_lowercase())),
                public_key_path: Some(format!("{}_public.pem", local_name.to_lowercase())),
                auto_generate_keys: true,
                default_security_level: "private".to_string(),
                trusted_domains: vec!["synapse.local".to_string()],
                require_encryption_for: vec!["human".to_string()],
            },
            logging: LoggingConfig {
                level: "info".to_string(),
                format: "compact".to_string(),
                file: None,
                log_message_content: false,
            },
        }
    }

    /// Get default capabilities for an entity type
    fn default_capabilities_for_type(entity_type: &str) -> Vec<String> {
        match entity_type {
            "ai_model" => vec![
                "conversation".to_string(),
                "analysis".to_string(),
                "reasoning".to_string(),
            ],
            "human" => vec![
                "conversation".to_string(),
                "decision_making".to_string(),
                "creativity".to_string(),
            ],
            "tool" => vec![
                "task_execution".to_string(),
                "data_processing".to_string(),
            ],
            "service" => vec![
                "api_access".to_string(),
                "data_storage".to_string(),
            ],
            "router" => vec![
                "message_routing".to_string(),
                "identity_resolution".to_string(),
            ],
            _ => vec!["communication".to_string()],
        }
    }

    /// Validate the configuration
    pub fn validate(&self) -> Result<()> {
        // Check entity configuration
        if self.entity.local_name.trim().is_empty() {
            return Err(ConfigError::ValidationFailed("Local name cannot be empty".to_string()).into());
        }

        if self.entity.domain.trim().is_empty() {
            return Err(ConfigError::ValidationFailed("Domain cannot be empty".to_string()).into());
        }

        // Check email configuration
        if self.email.smtp.host.trim().is_empty() {
            return Err(ConfigError::ValidationFailed("SMTP host cannot be empty".to_string()).into());
        }

        if self.email.imap.host.trim().is_empty() {
            return Err(ConfigError::ValidationFailed("IMAP host cannot be empty".to_string()).into());
        }

        // Check ports are valid
        if self.email.smtp.port == 0 {
            return Err(ConfigError::ValidationFailed("Invalid SMTP port".to_string()).into());
        }

        if self.email.imap.port == 0 {
            return Err(ConfigError::ValidationFailed("Invalid IMAP port".to_string()).into());
        }

        // Check security configuration
        if !["public", "private", "authenticated", "secure"].contains(&self.security.default_security_level.as_str()) {
            return Err(ConfigError::ValidationFailed("Invalid default security level".to_string()).into());
        }

        // Check logging configuration
        if !["trace", "debug", "info", "warn", "error"].contains(&self.logging.level.as_str()) {
            return Err(ConfigError::ValidationFailed("Invalid log level".to_string()).into());
        }

        if !["compact", "pretty", "json"].contains(&self.logging.format.as_str()) {
            return Err(ConfigError::ValidationFailed("Invalid log format".to_string()).into());
        }

        Ok(())
    }

    /// Create Gmail configuration
    pub fn gmail_config(local_name: impl Into<String>, entity_type: impl Into<String>, email: impl Into<String>, password: impl Into<String>) -> Self {
        let local_name = local_name.into();
        let entity_type = entity_type.into();
        let email = email.into();
        let password = password.into();

        let mut config = Self::default_for_entity(&local_name, &entity_type);
        
        config.email.smtp = SmtpConfig {
            host: "smtp.gmail.com".to_string(),
            port: 587,
            username: email.clone(),
            password: password.clone(),
            use_tls: true,
            use_ssl: false,
        };

        config.email.imap = ImapConfig {
            host: "imap.gmail.com".to_string(),
            port: 993,
            username: email,
            password,
            use_ssl: true,
        };

        config
    }

    /// Create Outlook configuration
    pub fn outlook_config(local_name: impl Into<String>, entity_type: impl Into<String>, email: impl Into<String>, password: impl Into<String>) -> Self {
        let local_name = local_name.into();
        let entity_type = entity_type.into();
        let email = email.into();
        let password = password.into();

        let mut config = Self::default_for_entity(&local_name, &entity_type);
        
        config.email.smtp = SmtpConfig {
            host: "smtp-mail.outlook.com".to_string(),
            port: 587,
            username: email.clone(),
            password: password.clone(),
            use_tls: true,
            use_ssl: false,
        };

        config.email.imap = ImapConfig {
            host: "outlook.office365.com".to_string(),
            port: 993,
            username: email,
            password,
            use_ssl: true,
        };

        config
    }

    /// Create a configuration suitable for testing
    pub fn for_testing() -> Self {
        Self::default_for_entity("test_entity", "test")
    }
}

impl Default for Config {
    fn default() -> Self {
        Self::default_for_entity("default", "ai_model")
    }
}

/// Predefined configurations for common providers
pub struct ConfigTemplates;

impl ConfigTemplates {
    /// Get all available provider templates
    pub fn available_providers() -> Vec<&'static str> {
        vec!["gmail", "outlook", "yahoo", "custom"]
    }

    /// Create config for a specific provider
    pub fn for_provider(
        provider: &str,
        local_name: impl Into<String>,
        entity_type: impl Into<String>,
        email: impl Into<String>,
        password: impl Into<String>,
    ) -> Result<Config> {
        match provider.to_lowercase().as_str() {
            "gmail" => Ok(Config::gmail_config(local_name, entity_type, email, password)),
            "outlook" => Ok(Config::outlook_config(local_name, entity_type, email, password)),
            "yahoo" => {
                let mut config = Config::outlook_config(local_name, entity_type, email, password);
                config.email.smtp.host = "smtp.mail.yahoo.com".to_string();
                config.email.imap.host = "imap.mail.yahoo.com".to_string();
                Ok(config)
            }
            "custom" => Ok(Config::default_for_entity(local_name, entity_type)),
            _ => Err(ConfigError::ValidationFailed(format!("Unknown provider: {}", provider)).into()),
        }
    }

    /// Get provider-specific help text
    pub fn provider_help(provider: &str) -> Option<&'static str> {
        match provider.to_lowercase().as_str() {
            "gmail" => Some(
                "Gmail Configuration:\n\
                - Enable 2-factor authentication\n\
                - Generate an App Password\n\
                - Use App Password instead of regular password\n\
                - Enable IMAP in Gmail settings"
            ),
            "outlook" => Some(
                "Outlook Configuration:\n\
                - Works with Microsoft 365 accounts\n\
                - Use your regular email and password\n\
                - May require app-specific password for some accounts"
            ),
            "yahoo" => Some(
                "Yahoo Configuration:\n\
                - Generate an App Password in Yahoo Account Security\n\
                - Use App Password instead of regular password\n\
                - Enable IMAP access in Yahoo Mail settings"
            ),
            _ => None,
        }
    }
}

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

    #[test]
    fn test_default_config_creation() {
        let config = Config::default_for_entity("Claude", "ai_model");
        assert_eq!(config.entity.local_name, "Claude");
        assert_eq!(config.entity.entity_type, "ai_model");
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_gmail_config() {
        let config = Config::gmail_config("Eric", "human", "eric@gmail.com", "app_password");
        assert_eq!(config.email.smtp.host, "smtp.gmail.com");
        assert_eq!(config.email.imap.host, "imap.gmail.com");
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_config_file_operations() {
        let config = Config::default_for_entity("Test", "tool");
        
        let temp_file = NamedTempFile::new().unwrap();
        
        // Save config
        assert!(config.to_file(temp_file.path()).is_ok());
        
        // Load config
        let loaded_config = Config::from_file(temp_file.path()).unwrap();
        assert_eq!(config.entity.local_name, loaded_config.entity.local_name);
    }

    #[test]
    fn test_config_validation() {
        let mut config = Config::default_for_entity("Test", "ai_model");
        
        // Valid config should pass
        assert!(config.validate().is_ok());
        
        // Invalid entity name should fail
        config.entity.local_name = "".to_string();
        assert!(config.validate().is_err());
        
        // Fix entity name
        config.entity.local_name = "Test".to_string();
        assert!(config.validate().is_ok());
        
        // Test config with invalid values should fail
        let mut invalid_config = config.clone();
        invalid_config.email.smtp.host = "".to_string(); // Empty host
        assert!(invalid_config.validate().is_err());
    }

    #[test]
    fn test_provider_templates() {
        let providers = ConfigTemplates::available_providers();
        assert!(providers.contains(&"gmail"));
        assert!(providers.contains(&"outlook"));

        let gmail_config = ConfigTemplates::for_provider("gmail", "Test", "ai_model", "test@gmail.com", "password");
        assert!(gmail_config.is_ok());
        
        let unknown_config = ConfigTemplates::for_provider("unknown", "Test", "ai_model", "test@example.com", "password");
        assert!(unknown_config.is_err());
    }
}