chasm-cli 2.0.0

Universal chat session manager - harvest, merge, and analyze AI chat history from VS Code, Cursor, and other editors
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
// Copyright (c) 2024-2027 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! White-labeling and custom branding
//!
//! Supports tenant-specific branding, themes, and customization.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

// ============================================================================
// Branding Configuration
// ============================================================================

/// Complete branding configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BrandingConfig {
    /// Unique branding ID
    pub id: Uuid,
    /// Tenant ID
    pub tenant_id: Uuid,
    /// Brand name
    pub brand_name: String,
    /// Logo configuration
    pub logo: LogoConfig,
    /// Color theme
    pub theme: ThemeConfig,
    /// Typography
    pub typography: TypographyConfig,
    /// Custom CSS
    pub custom_css: Option<String>,
    /// Email templates
    pub email_templates: EmailTemplates,
    /// Custom domain
    pub custom_domain: Option<CustomDomain>,
    /// Footer configuration
    pub footer: FooterConfig,
    /// Favicon URL
    pub favicon_url: Option<String>,
    /// Meta tags
    pub meta_tags: MetaTags,
    /// Feature visibility
    pub feature_visibility: FeatureVisibility,
    /// Created at
    pub created_at: DateTime<Utc>,
    /// Updated at
    pub updated_at: DateTime<Utc>,
}

impl BrandingConfig {
    /// Create default branding for a tenant
    pub fn default_for_tenant(tenant_id: Uuid, brand_name: &str) -> Self {
        Self {
            id: Uuid::new_v4(),
            tenant_id,
            brand_name: brand_name.to_string(),
            logo: LogoConfig::default(),
            theme: ThemeConfig::default(),
            typography: TypographyConfig::default(),
            custom_css: None,
            email_templates: EmailTemplates::default(),
            custom_domain: None,
            footer: FooterConfig::default(),
            favicon_url: None,
            meta_tags: MetaTags::default(),
            feature_visibility: FeatureVisibility::default(),
            created_at: Utc::now(),
            updated_at: Utc::now(),
        }
    }
}

// ============================================================================
// Logo Configuration
// ============================================================================

/// Logo configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogoConfig {
    /// Primary logo URL
    pub primary_url: Option<String>,
    /// Dark mode logo URL
    pub dark_mode_url: Option<String>,
    /// Icon/favicon URL
    pub icon_url: Option<String>,
    /// Logo alt text
    pub alt_text: String,
    /// Logo width (px)
    pub width: Option<u32>,
    /// Logo height (px)
    pub height: Option<u32>,
}

impl Default for LogoConfig {
    fn default() -> Self {
        Self {
            primary_url: None,
            dark_mode_url: None,
            icon_url: None,
            alt_text: "Logo".to_string(),
            width: None,
            height: Some(40),
        }
    }
}

// ============================================================================
// Theme Configuration
// ============================================================================

/// Color theme configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThemeConfig {
    /// Primary brand color
    pub primary_color: String,
    /// Secondary color
    pub secondary_color: String,
    /// Accent color
    pub accent_color: String,
    /// Background color
    pub background_color: String,
    /// Surface/card color
    pub surface_color: String,
    /// Text color
    pub text_color: String,
    /// Secondary text color
    pub text_secondary_color: String,
    /// Border color
    pub border_color: String,
    /// Success color
    pub success_color: String,
    /// Warning color
    pub warning_color: String,
    /// Error color
    pub error_color: String,
    /// Info color
    pub info_color: String,
    /// Dark mode variant
    pub dark_mode: Option<Box<ThemeConfig>>,
    /// Border radius
    pub border_radius: String,
    /// Shadow style
    pub shadow: String,
}

impl Default for ThemeConfig {
    fn default() -> Self {
        Self {
            primary_color: "#2563eb".to_string(),
            secondary_color: "#7c3aed".to_string(),
            accent_color: "#06b6d4".to_string(),
            background_color: "#ffffff".to_string(),
            surface_color: "#f9fafb".to_string(),
            text_color: "#111827".to_string(),
            text_secondary_color: "#6b7280".to_string(),
            border_color: "#e5e7eb".to_string(),
            success_color: "#059669".to_string(),
            warning_color: "#d97706".to_string(),
            error_color: "#dc2626".to_string(),
            info_color: "#2563eb".to_string(),
            dark_mode: Some(Box::new(Self::dark_mode_defaults())),
            border_radius: "0.5rem".to_string(),
            shadow: "0 1px 3px 0 rgb(0 0 0 / 0.1)".to_string(),
        }
    }
}

impl ThemeConfig {
    /// Dark mode defaults
    pub fn dark_mode_defaults() -> Self {
        Self {
            primary_color: "#3b82f6".to_string(),
            secondary_color: "#8b5cf6".to_string(),
            accent_color: "#22d3ee".to_string(),
            background_color: "#0f172a".to_string(),
            surface_color: "#1e293b".to_string(),
            text_color: "#f1f5f9".to_string(),
            text_secondary_color: "#94a3b8".to_string(),
            border_color: "#334155".to_string(),
            success_color: "#10b981".to_string(),
            warning_color: "#f59e0b".to_string(),
            error_color: "#ef4444".to_string(),
            info_color: "#3b82f6".to_string(),
            dark_mode: None,
            border_radius: "0.5rem".to_string(),
            shadow: "0 1px 3px 0 rgb(0 0 0 / 0.3)".to_string(),
        }
    }

    /// Generate CSS variables
    pub fn to_css_variables(&self) -> String {
        format!(
            r#":root {{
  --color-primary: {};
  --color-secondary: {};
  --color-accent: {};
  --color-background: {};
  --color-surface: {};
  --color-text: {};
  --color-text-secondary: {};
  --color-border: {};
  --color-success: {};
  --color-warning: {};
  --color-error: {};
  --color-info: {};
  --border-radius: {};
  --shadow: {};
}}"#,
            self.primary_color,
            self.secondary_color,
            self.accent_color,
            self.background_color,
            self.surface_color,
            self.text_color,
            self.text_secondary_color,
            self.border_color,
            self.success_color,
            self.warning_color,
            self.error_color,
            self.info_color,
            self.border_radius,
            self.shadow
        )
    }
}

// ============================================================================
// Typography
// ============================================================================

/// Typography configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypographyConfig {
    /// Primary font family
    pub font_family: String,
    /// Heading font family
    pub heading_font_family: Option<String>,
    /// Monospace font family
    pub mono_font_family: String,
    /// Base font size
    pub base_font_size: String,
    /// Line height
    pub line_height: String,
    /// Font weights
    pub font_weights: FontWeights,
    /// Custom font URLs
    pub custom_font_urls: Vec<String>,
}

impl Default for TypographyConfig {
    fn default() -> Self {
        Self {
            font_family: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif".to_string(),
            heading_font_family: None,
            mono_font_family: "ui-monospace, SFMono-Regular, Menlo, Monaco, monospace".to_string(),
            base_font_size: "16px".to_string(),
            line_height: "1.5".to_string(),
            font_weights: FontWeights::default(),
            custom_font_urls: vec![],
        }
    }
}

/// Font weights
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FontWeights {
    pub light: u16,
    pub regular: u16,
    pub medium: u16,
    pub semibold: u16,
    pub bold: u16,
}

impl Default for FontWeights {
    fn default() -> Self {
        Self {
            light: 300,
            regular: 400,
            medium: 500,
            semibold: 600,
            bold: 700,
        }
    }
}

// ============================================================================
// Email Templates
// ============================================================================

/// Email templates for white-labeled communications
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmailTemplates {
    /// Welcome email template
    pub welcome: EmailTemplate,
    /// Invitation email template
    pub invitation: EmailTemplate,
    /// Password reset email template
    pub password_reset: EmailTemplate,
    /// Session shared notification
    pub session_shared: EmailTemplate,
    /// Weekly digest
    pub weekly_digest: EmailTemplate,
    /// Custom templates
    pub custom: HashMap<String, EmailTemplate>,
}

impl Default for EmailTemplates {
    fn default() -> Self {
        Self {
            welcome: EmailTemplate::default_welcome(),
            invitation: EmailTemplate::default_invitation(),
            password_reset: EmailTemplate::default_password_reset(),
            session_shared: EmailTemplate::default_session_shared(),
            weekly_digest: EmailTemplate::default_digest(),
            custom: HashMap::new(),
        }
    }
}

/// Email template
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmailTemplate {
    /// Subject line (supports variables)
    pub subject: String,
    /// HTML body
    pub body_html: String,
    /// Plain text body
    pub body_text: String,
    /// From name
    pub from_name: Option<String>,
    /// Reply-to address
    pub reply_to: Option<String>,
}

impl EmailTemplate {
    fn default_welcome() -> Self {
        Self {
            subject: "Welcome to {{brand_name}}".to_string(),
            body_html: r#"<h1>Welcome to {{brand_name}}</h1><p>Hello {{user_name}},</p><p>Your account has been created.</p>"#.to_string(),
            body_text: "Welcome to {{brand_name}}\n\nHello {{user_name}},\nYour account has been created.".to_string(),
            from_name: None,
            reply_to: None,
        }
    }

    fn default_invitation() -> Self {
        Self {
            subject: "You've been invited to {{brand_name}}".to_string(),
            body_html: r#"<h1>You're invited!</h1><p>{{inviter_name}} has invited you to join {{brand_name}}.</p>"#.to_string(),
            body_text: "You're invited!\n\n{{inviter_name}} has invited you to join {{brand_name}}.".to_string(),
            from_name: None,
            reply_to: None,
        }
    }

    fn default_password_reset() -> Self {
        Self {
            subject: "Reset your {{brand_name}} password".to_string(),
            body_html: r#"<h1>Password Reset</h1><p>Click the link below to reset your password.</p>"#.to_string(),
            body_text: "Password Reset\n\nClick the link below to reset your password.".to_string(),
            from_name: None,
            reply_to: None,
        }
    }

    fn default_session_shared() -> Self {
        Self {
            subject: "{{sharer_name}} shared a session with you".to_string(),
            body_html: r#"<h1>Session Shared</h1><p>{{sharer_name}} shared "{{session_title}}" with you.</p>"#.to_string(),
            body_text: "Session Shared\n\n{{sharer_name}} shared \"{{session_title}}\" with you.".to_string(),
            from_name: None,
            reply_to: None,
        }
    }

    fn default_digest() -> Self {
        Self {
            subject: "Your weekly {{brand_name}} digest".to_string(),
            body_html: r#"<h1>Weekly Digest</h1><p>Here's your activity summary.</p>"#.to_string(),
            body_text: "Weekly Digest\n\nHere's your activity summary.".to_string(),
            from_name: None,
            reply_to: None,
        }
    }

    /// Render template with variables
    pub fn render(&self, variables: &HashMap<String, String>) -> (String, String, String) {
        let mut subject = self.subject.clone();
        let mut body_html = self.body_html.clone();
        let mut body_text = self.body_text.clone();

        for (key, value) in variables {
            let placeholder = format!("{{{{{}}}}}", key);
            subject = subject.replace(&placeholder, value);
            body_html = body_html.replace(&placeholder, value);
            body_text = body_text.replace(&placeholder, value);
        }

        (subject, body_html, body_text)
    }
}

// ============================================================================
// Custom Domain
// ============================================================================

/// Custom domain configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomDomain {
    /// Domain name
    pub domain: String,
    /// SSL certificate status
    pub ssl_status: SslStatus,
    /// DNS verification status
    pub dns_verified: bool,
    /// DNS verification token
    pub dns_token: String,
    /// CNAME target
    pub cname_target: String,
    /// Configured at
    pub configured_at: DateTime<Utc>,
    /// SSL expires at
    pub ssl_expires_at: Option<DateTime<Utc>>,
}

/// SSL status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SslStatus {
    Pending,
    Provisioning,
    Active,
    Failed,
    Expired,
}

// ============================================================================
// Footer Configuration
// ============================================================================

/// Footer configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FooterConfig {
    /// Show powered by
    pub show_powered_by: bool,
    /// Powered by text
    pub powered_by_text: Option<String>,
    /// Copyright text
    pub copyright_text: Option<String>,
    /// Footer links
    pub links: Vec<FooterLink>,
    /// Social links
    pub social_links: Vec<SocialLink>,
}

impl Default for FooterConfig {
    fn default() -> Self {
        Self {
            show_powered_by: true,
            powered_by_text: None,
            copyright_text: None,
            links: vec![],
            social_links: vec![],
        }
    }
}

/// Footer link
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FooterLink {
    pub label: String,
    pub url: String,
    pub new_tab: bool,
}

/// Social link
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SocialLink {
    pub platform: SocialPlatform,
    pub url: String,
}

/// Social platform
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SocialPlatform {
    Twitter,
    LinkedIn,
    GitHub,
    Facebook,
    Instagram,
    YouTube,
    Discord,
    Slack,
}

// ============================================================================
// Meta Tags
// ============================================================================

/// Meta tags for SEO
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetaTags {
    /// Page title template
    pub title_template: String,
    /// Default description
    pub description: String,
    /// Keywords
    pub keywords: Vec<String>,
    /// Open Graph image URL
    pub og_image_url: Option<String>,
    /// Twitter card type
    pub twitter_card: String,
    /// Additional meta tags
    pub custom: HashMap<String, String>,
}

impl Default for MetaTags {
    fn default() -> Self {
        Self {
            title_template: "{{page_title}} | {{brand_name}}".to_string(),
            description: "AI chat session management platform".to_string(),
            keywords: vec![],
            og_image_url: None,
            twitter_card: "summary_large_image".to_string(),
            custom: HashMap::new(),
        }
    }
}

// ============================================================================
// Feature Visibility
// ============================================================================

/// Control visibility of UI features
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeatureVisibility {
    /// Show provider logos
    pub show_provider_logos: bool,
    /// Show analytics
    pub show_analytics: bool,
    /// Show collaboration features
    pub show_collaboration: bool,
    /// Show export options
    pub show_export: bool,
    /// Show API documentation
    pub show_api_docs: bool,
    /// Show help/support
    pub show_help: bool,
    /// Custom hidden features
    pub hidden_features: Vec<String>,
}

impl Default for FeatureVisibility {
    fn default() -> Self {
        Self {
            show_provider_logos: true,
            show_analytics: true,
            show_collaboration: true,
            show_export: true,
            show_api_docs: true,
            show_help: true,
            hidden_features: vec![],
        }
    }
}

// ============================================================================
// Branding Manager
// ============================================================================

/// Manages branding configurations
pub struct BrandingManager {
    /// Branding configs by tenant
    configs: HashMap<Uuid, BrandingConfig>,
}

impl BrandingManager {
    /// Create a new branding manager
    pub fn new() -> Self {
        Self {
            configs: HashMap::new(),
        }
    }

    /// Create branding for a tenant
    pub fn create_branding(&mut self, tenant_id: Uuid, brand_name: &str) -> BrandingConfig {
        let config = BrandingConfig::default_for_tenant(tenant_id, brand_name);
        self.configs.insert(tenant_id, config.clone());
        config
    }

    /// Get branding for a tenant
    pub fn get_branding(&self, tenant_id: Uuid) -> Option<&BrandingConfig> {
        self.configs.get(&tenant_id)
    }

    /// Update theme
    pub fn update_theme(&mut self, tenant_id: Uuid, theme: ThemeConfig) -> bool {
        if let Some(config) = self.configs.get_mut(&tenant_id) {
            config.theme = theme;
            config.updated_at = Utc::now();
            true
        } else {
            false
        }
    }

    /// Update logo
    pub fn update_logo(&mut self, tenant_id: Uuid, logo: LogoConfig) -> bool {
        if let Some(config) = self.configs.get_mut(&tenant_id) {
            config.logo = logo;
            config.updated_at = Utc::now();
            true
        } else {
            false
        }
    }

    /// Set custom domain
    pub fn set_custom_domain(&mut self, tenant_id: Uuid, domain: &str) -> Option<CustomDomain> {
        let config = self.configs.get_mut(&tenant_id)?;
        
        let custom_domain = CustomDomain {
            domain: domain.to_string(),
            ssl_status: SslStatus::Pending,
            dns_verified: false,
            dns_token: Uuid::new_v4().to_string(),
            cname_target: "app.chasm.cloud".to_string(),
            configured_at: Utc::now(),
            ssl_expires_at: None,
        };

        config.custom_domain = Some(custom_domain.clone());
        config.updated_at = Utc::now();

        Some(custom_domain)
    }

    /// Generate CSS for tenant
    pub fn generate_css(&self, tenant_id: Uuid) -> Option<String> {
        let config = self.configs.get(&tenant_id)?;
        let mut css = config.theme.to_css_variables();

        if let Some(ref custom_css) = config.custom_css {
            css.push_str("\n\n/* Custom CSS */\n");
            css.push_str(custom_css);
        }

        Some(css)
    }
}

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

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

    #[test]
    fn test_theme_css_variables() {
        let theme = ThemeConfig::default();
        let css = theme.to_css_variables();
        
        assert!(css.contains("--color-primary"));
        assert!(css.contains("#2563eb"));
    }

    #[test]
    fn test_email_template_render() {
        let template = EmailTemplate::default_welcome();
        let mut vars = HashMap::new();
        vars.insert("brand_name".to_string(), "Acme".to_string());
        vars.insert("user_name".to_string(), "John".to_string());

        let (subject, _, _) = template.render(&vars);
        assert_eq!(subject, "Welcome to Acme");
    }

    #[test]
    fn test_branding_manager() {
        let mut manager = BrandingManager::new();
        let tenant_id = Uuid::new_v4();

        let config = manager.create_branding(tenant_id, "Test Brand");
        assert_eq!(config.brand_name, "Test Brand");

        assert!(manager.get_branding(tenant_id).is_some());
    }
}