vkteams-bot 0.11.5

High-performance VK Teams Bot API toolkit with CLI and MCP server support
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
//! Unified configuration for CLI and MCP components
//!
//! This module provides shared configuration structures that can be used
//! by both the CLI and MCP server components, ensuring consistency.

use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::path::PathBuf;

/// Unified configuration structure for both CLI and MCP
#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct UnifiedConfig {
    /// API configuration shared by CLI and MCP
    #[serde(default)]
    pub api: ApiConfig,

    /// Storage configuration (database and vector search)
    #[cfg(feature = "storage")]
    #[serde(default)]
    pub storage: StorageConfig,

    /// MCP server specific configuration
    #[serde(default)]
    pub mcp: McpConfig,

    /// CLI specific configuration
    #[serde(default)]
    pub cli: CliConfig,

    /// Network configuration shared by both components
    #[serde(default)]
    pub network: NetworkConfig,

    /// OpenTelemetry configuration
    #[cfg(feature = "otlp")]
    #[serde(default)]
    pub otlp: OtlpConfig,
}

/// API configuration
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct ApiConfig {
    /// VK Teams Bot API token
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token: Option<String>,

    /// API base URL
    #[serde(default = "default_api_url")]
    pub url: String,

    /// Request timeout in seconds
    #[serde(default = "default_timeout")]
    pub timeout: u64,

    /// Maximum retry attempts
    #[serde(default = "default_retries")]
    pub max_retries: u32,
}

impl Default for ApiConfig {
    fn default() -> Self {
        Self {
            token: None,
            url: default_api_url(),
            timeout: default_timeout(),
            max_retries: default_retries(),
        }
    }
}

/// Storage configuration
#[cfg(feature = "storage")]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "snake_case")]
pub struct StorageConfig {
    /// Database configuration
    #[serde(default)]
    pub database: DatabaseConfig,

    /// Vector embedding configuration
    #[cfg(feature = "vector-search")]
    #[serde(default)]
    pub embedding: EmbeddingConfig,
}

/// Database configuration
#[cfg(feature = "storage")]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct DatabaseConfig {
    /// Database connection URL
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,

    /// Maximum number of connections
    #[serde(default = "default_max_connections")]
    pub max_connections: u32,

    /// Whether to auto-migrate on startup
    #[serde(default = "default_auto_migrate")]
    pub auto_migrate: bool,
}

#[cfg(feature = "storage")]
impl Default for DatabaseConfig {
    fn default() -> Self {
        Self {
            url: None,
            max_connections: default_max_connections(),
            auto_migrate: default_auto_migrate(),
        }
    }
}

/// Vector embedding configuration
#[cfg(all(feature = "storage", feature = "vector-search"))]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct EmbeddingConfig {
    /// Embedding provider (ollama, openai)
    #[serde(default = "default_embedding_provider")]
    pub provider: String,

    /// Model name
    #[serde(default = "default_embedding_model")]
    pub model: String,

    /// API endpoint (for ollama or custom endpoints)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub endpoint: Option<String>,

    /// API key (for OpenAI and similar services)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub api_key: Option<String>,
}

#[cfg(all(feature = "storage", feature = "vector-search"))]
impl Default for EmbeddingConfig {
    fn default() -> Self {
        Self {
            provider: default_embedding_provider(),
            model: default_embedding_model(),
            endpoint: None,
            api_key: None,
        }
    }
}

/// MCP server configuration
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct McpConfig {
    /// Default chat ID for MCP operations
    #[serde(skip_serializing_if = "Option::is_none")]
    pub chat_id: Option<String>,

    /// Path to CLI binary
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cli_path: Option<PathBuf>,

    /// Enable storage tools in MCP
    #[serde(default = "default_enable_storage_tools")]
    pub enable_storage_tools: bool,

    /// Enable file tools in MCP
    #[serde(default = "default_enable_file_tools")]
    pub enable_file_tools: bool,

    /// Maximum retry attempts for CLI calls
    #[serde(default = "default_cli_retries")]
    pub cli_retries: usize,
}

impl Default for McpConfig {
    fn default() -> Self {
        Self {
            chat_id: None,
            cli_path: None,
            enable_storage_tools: default_enable_storage_tools(),
            enable_file_tools: default_enable_file_tools(),
            cli_retries: default_cli_retries(),
        }
    }
}

/// CLI specific configuration
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "snake_case")]
pub struct CliConfig {
    /// File handling configuration
    #[serde(default)]
    pub files: FileConfig,

    /// Logging configuration
    #[serde(default)]
    pub logging: LoggingConfig,

    /// UI configuration
    #[serde(default)]
    pub ui: UiConfig,
}

/// File handling configuration
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct FileConfig {
    /// Download directory
    #[serde(skip_serializing_if = "Option::is_none")]
    pub download_dir: Option<PathBuf>,

    /// Upload directory
    #[serde(skip_serializing_if = "Option::is_none")]
    pub upload_dir: Option<PathBuf>,

    /// Maximum file size in bytes
    #[serde(default = "default_max_file_size")]
    pub max_file_size: usize,
}

impl Default for FileConfig {
    fn default() -> Self {
        Self {
            download_dir: None,
            upload_dir: None,
            max_file_size: default_max_file_size(),
        }
    }
}

/// Logging configuration
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct LoggingConfig {
    /// Log level
    #[serde(default = "default_log_level")]
    pub level: String,

    /// Log format
    #[serde(default = "default_log_format")]
    pub format: String,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: default_log_level(),
            format: default_log_format(),
        }
    }
}

/// UI configuration
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct UiConfig {
    /// Show progress bars
    #[serde(default = "default_show_progress")]
    pub show_progress: bool,

    /// Use colored output
    #[serde(default = "default_colored_output")]
    pub colored_output: bool,
}

impl Default for UiConfig {
    fn default() -> Self {
        Self {
            show_progress: default_show_progress(),
            colored_output: default_colored_output(),
        }
    }
}

/// Network configuration
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct NetworkConfig {
    /// Number of retry attempts
    #[serde(default = "default_retries_usize")]
    pub retries: usize,

    /// Request timeout in seconds
    #[serde(default = "default_request_timeout")]
    pub request_timeout_secs: u64,

    /// Connection timeout in seconds
    #[serde(default = "default_connect_timeout")]
    pub connect_timeout_secs: u64,
}

impl Default for NetworkConfig {
    fn default() -> Self {
        Self {
            retries: default_retries_usize(),
            request_timeout_secs: default_request_timeout(),
            connect_timeout_secs: default_connect_timeout(),
        }
    }
}

/// OpenTelemetry configuration
#[cfg(feature = "otlp")]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct OtlpConfig {
    /// Instance ID
    #[serde(default = "default_instance_id")]
    pub instance_id: Cow<'static, str>,

    /// Deployment environment
    #[serde(default = "default_environment")]
    pub deployment_environment_name: Cow<'static, str>,

    /// OTLP exporter endpoint
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exporter_endpoint: Option<String>,
}

#[cfg(feature = "otlp")]
impl Default for OtlpConfig {
    fn default() -> Self {
        Self {
            instance_id: default_instance_id(),
            deployment_environment_name: default_environment(),
            exporter_endpoint: None,
        }
    }
}

// Default value functions
fn default_api_url() -> String {
    "https://api.vk.com".to_string()
}

fn default_timeout() -> u64 {
    30
}

fn default_retries() -> u32 {
    3
}

fn default_retries_usize() -> usize {
    3
}

#[cfg(feature = "storage")]
fn default_max_connections() -> u32 {
    20
}

#[cfg(feature = "storage")]
fn default_auto_migrate() -> bool {
    true
}

#[cfg(all(feature = "storage", feature = "vector-search"))]
fn default_embedding_provider() -> String {
    "ollama".to_string()
}

#[cfg(all(feature = "storage", feature = "vector-search"))]
fn default_embedding_model() -> String {
    "nomic-embed-text".to_string()
}

fn default_enable_storage_tools() -> bool {
    true
}

fn default_enable_file_tools() -> bool {
    true
}

fn default_cli_retries() -> usize {
    3
}

fn default_max_file_size() -> usize {
    104_857_600 // 100MB
}

fn default_log_level() -> String {
    "info".to_string()
}

fn default_log_format() -> String {
    "pretty".to_string()
}

fn default_show_progress() -> bool {
    true
}

fn default_colored_output() -> bool {
    true
}

fn default_request_timeout() -> u64 {
    30
}

fn default_connect_timeout() -> u64 {
    10
}

#[cfg(feature = "otlp")]
fn default_instance_id() -> Cow<'static, str> {
    Cow::Borrowed("bot")
}

#[cfg(feature = "otlp")]
fn default_environment() -> Cow<'static, str> {
    Cow::Borrowed("production")
}

impl UnifiedConfig {
    /// Load configuration from file or environment variables
    pub fn load_from_file<P: AsRef<std::path::Path>>(
        path: P,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let content = std::fs::read_to_string(path)?;
        let mut config: UnifiedConfig = toml::from_str(&content)?;

        // Override with environment variables
        config.apply_env_overrides();

        Ok(config)
    }

    /// Apply environment variable overrides
    pub fn apply_env_overrides(&mut self) {
        // API configuration
        if let Ok(token) = std::env::var("VKTEAMS_BOT_API_TOKEN") {
            self.api.token = Some(token);
        }
        if let Ok(url) = std::env::var("VKTEAMS_BOT_API_URL") {
            self.api.url = url;
        }

        // MCP configuration
        if let Ok(chat_id) = std::env::var("VKTEAMS_BOT_CHAT_ID") {
            self.mcp.chat_id = Some(chat_id);
        }
        if let Ok(cli_path) = std::env::var("VKTEAMS_BOT_CLI_PATH") {
            self.mcp.cli_path = Some(PathBuf::from(cli_path));
        }

        // Storage configuration
        #[cfg(feature = "storage")]
        if let Ok(db_url) = std::env::var("DATABASE_URL") {
            self.storage.database.url = Some(db_url);
        }

        // Embedding configuration
        #[cfg(all(feature = "storage", feature = "vector-search"))]
        {
            if let Ok(provider) = std::env::var("EMBEDDING_PROVIDER") {
                self.storage.embedding.provider = provider;
            }
            if let Ok(model) = std::env::var("EMBEDDING_MODEL") {
                self.storage.embedding.model = model;
            }
            if let Ok(endpoint) = std::env::var("EMBEDDING_ENDPOINT") {
                self.storage.embedding.endpoint = Some(endpoint);
            }
            if let Ok(api_key) = std::env::var("EMBEDDING_API_KEY") {
                self.storage.embedding.api_key = Some(api_key);
            }
        }
    }

    /// Create a default configuration file
    pub fn create_default_file<P: AsRef<std::path::Path>>(
        path: P,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let config = UnifiedConfig::default();
        let content = toml::to_string_pretty(&config)?;
        std::fs::write(path, content)?;
        Ok(())
    }
}

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

    #[test]
    fn test_default_config() {
        let config = UnifiedConfig::default();
        assert_eq!(config.api.url, "https://api.vk.com");
        assert_eq!(config.api.timeout, 30);
        assert_eq!(config.network.retries, 3);
        assert!(config.mcp.enable_storage_tools);
        assert!(config.cli.ui.show_progress);
    }

    #[test]
    fn test_serialize_deserialize() {
        let config = UnifiedConfig::default();
        let toml_str = toml::to_string(&config).unwrap();
        let deserialized: UnifiedConfig = toml::from_str(&toml_str).unwrap();
        assert_eq!(config, deserialized);
    }

    #[test]
    fn test_env_overrides() {
        unsafe {
            std::env::set_var("VKTEAMS_BOT_API_TOKEN", "test_token");
            std::env::set_var("VKTEAMS_BOT_CHAT_ID", "test_chat");
        }

        let mut config = UnifiedConfig::default();
        config.apply_env_overrides();

        assert_eq!(config.api.token, Some("test_token".to_string()));
        assert_eq!(config.mcp.chat_id, Some("test_chat".to_string()));

        // Cleanup
        unsafe {
            std::env::remove_var("VKTEAMS_BOT_API_TOKEN");
            std::env::remove_var("VKTEAMS_BOT_CHAT_ID");
        }
    }

    #[test]
    fn test_api_config_default() {
        let api_config = ApiConfig::default();
        assert_eq!(api_config.url, "https://api.vk.com");
        assert_eq!(api_config.timeout, 30);
        assert!(api_config.token.is_none());
        assert_eq!(api_config.max_retries, 3);
    }

    #[test]
    fn test_network_config_default() {
        let network_config = NetworkConfig::default();
        assert_eq!(network_config.retries, 3);
        assert_eq!(network_config.request_timeout_secs, 30);
        assert_eq!(network_config.connect_timeout_secs, 10);
    }

    #[test]
    fn test_mcp_config_default() {
        let mcp_config = McpConfig::default();
        assert!(mcp_config.chat_id.is_none());
        assert!(mcp_config.cli_path.is_none());
        assert!(mcp_config.enable_storage_tools);
        assert!(mcp_config.enable_file_tools);
        assert_eq!(mcp_config.cli_retries, 3);
    }

    #[test]
    fn test_cli_config_default() {
        let cli_config = CliConfig::default();
        assert!(cli_config.ui.show_progress);
    }

    #[test]
    fn test_api_config_custom() {
        let api_config = ApiConfig {
            url: "https://custom.api.com".to_string(),
            token: Some("custom_token".to_string()),
            timeout: 60,
            max_retries: 5,
        };

        assert_eq!(api_config.url, "https://custom.api.com");
        assert_eq!(api_config.token, Some("custom_token".to_string()));
        assert_eq!(api_config.timeout, 60);
        assert_eq!(api_config.max_retries, 5);
    }

    #[test]
    fn test_mcp_config_custom() {
        let mcp_config = McpConfig {
            chat_id: Some("custom_chat".to_string()),
            cli_path: Some(PathBuf::from("/usr/bin/cli")),
            enable_storage_tools: false,
            enable_file_tools: false,
            cli_retries: 5,
        };

        assert_eq!(mcp_config.chat_id, Some("custom_chat".to_string()));
        assert_eq!(mcp_config.cli_path, Some(PathBuf::from("/usr/bin/cli")));
        assert!(!mcp_config.enable_storage_tools);
        assert!(!mcp_config.enable_file_tools);
        assert_eq!(mcp_config.cli_retries, 5);
    }

    #[test]
    fn test_config_serialization_with_custom_values() {
        let mut config = UnifiedConfig::default();
        config.api.url = "https://custom.com".to_string();
        config.api.token = Some("secret".to_string());
        config.mcp.chat_id = Some("chat123".to_string());
        config.network.retries = 5;

        let serialized = toml::to_string(&config).unwrap();
        assert!(serialized.contains("https://custom.com"));
        assert!(serialized.contains("secret"));
        assert!(serialized.contains("chat123"));
        assert!(serialized.contains("retries = 5"));

        let deserialized: UnifiedConfig = toml::from_str(&serialized).unwrap();
        assert_eq!(config, deserialized);
    }

    #[test]
    fn test_env_override_partial() {
        unsafe {
            std::env::set_var("VKTEAMS_BOT_API_TOKEN", "env_token");
            // Don't set VKTEAMS_BOT_CHAT_ID to test partial override
        }

        let mut config = UnifiedConfig::default();
        config.mcp.chat_id = Some("original_chat".to_string());
        config.apply_env_overrides();

        // Token should be overridden
        assert_eq!(config.api.token, Some("env_token".to_string()));
        // Chat ID should remain original since env var not set
        assert_eq!(config.mcp.chat_id, Some("original_chat".to_string()));

        // Cleanup
        unsafe {
            std::env::remove_var("VKTEAMS_BOT_API_TOKEN");
        }
    }

    #[test]
    fn test_default_functions() {
        // Test the key default functions
        assert_eq!(default_api_url(), "https://api.vk.com");
        assert_eq!(default_timeout(), 30);
        assert_eq!(default_retries(), 3);
        assert!(default_enable_storage_tools());
        assert!(default_enable_file_tools());
        assert_eq!(default_cli_retries(), 3);
    }

    #[test]
    fn test_file_config_default() {
        let file_config = FileConfig::default();
        assert_eq!(file_config.max_file_size, 104_857_600); // 100MB
    }

    #[test]
    fn test_logging_config_default() {
        let logging_config = LoggingConfig::default();
        assert_eq!(logging_config.level, "info");
        assert_eq!(logging_config.format, "pretty");
    }

    #[test]
    fn test_ui_config_default() {
        let ui_config = UiConfig::default();
        assert!(ui_config.show_progress);
    }

    #[test]
    fn test_config_equality() {
        let config1 = UnifiedConfig::default();
        let config2 = UnifiedConfig::default();
        assert_eq!(config1, config2);

        let mut config3 = UnifiedConfig::default();
        config3.api.timeout = 60;
        assert_ne!(config1, config3);
    }

    #[test]
    fn test_config_cloning() {
        let mut original = UnifiedConfig::default();
        original.api.token = Some("test_token".to_string());
        original.mcp.chat_id = Some("test_chat".to_string());

        let cloned = original.clone();
        assert_eq!(original, cloned);
        assert_eq!(cloned.api.token, Some("test_token".to_string()));
        assert_eq!(cloned.mcp.chat_id, Some("test_chat".to_string()));
    }

    #[test]
    fn test_config_debug_format() {
        let config = UnifiedConfig::default();
        let debug_str = format!("{config:?}");
        assert!(debug_str.contains("UnifiedConfig"));
        assert!(debug_str.contains("ApiConfig"));
        assert!(debug_str.contains("NetworkConfig"));
        assert!(debug_str.contains("McpConfig"));
        assert!(debug_str.contains("CliConfig"));
    }
}