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
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! Cloud Sync Service Integration
//!
//! This module provides integration with cloud storage services for session backup
//! and cross-device synchronization.

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

// =============================================================================
// Cloud Provider Types
// =============================================================================

/// Supported cloud storage providers
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CloudProvider {
    /// Local file system (no cloud)
    Local,
    /// Amazon S3 compatible storage
    S3,
    /// Azure Blob Storage
    AzureBlob,
    /// Google Cloud Storage
    Gcs,
    /// Dropbox
    Dropbox,
    /// iCloud Drive
    ICloud,
    /// OneDrive
    OneDrive,
    /// Self-hosted WebDAV
    WebDav,
}

impl CloudProvider {
    /// Get display name
    pub fn display_name(&self) -> &'static str {
        match self {
            Self::Local => "Local Storage",
            Self::S3 => "Amazon S3",
            Self::AzureBlob => "Azure Blob Storage",
            Self::Gcs => "Google Cloud Storage",
            Self::Dropbox => "Dropbox",
            Self::ICloud => "iCloud Drive",
            Self::OneDrive => "OneDrive",
            Self::WebDav => "WebDAV",
        }
    }
}

// =============================================================================
// Cloud Sync Configuration
// =============================================================================

/// Cloud sync configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CloudSyncConfig {
    /// Whether cloud sync is enabled
    pub enabled: bool,
    /// Cloud provider
    pub provider: CloudProvider,
    /// Provider-specific configuration
    pub provider_config: ProviderSpecificConfig,
    /// Sync frequency in seconds (0 = manual only)
    pub sync_frequency_seconds: u64,
    /// Whether to sync automatically on session save
    pub auto_sync: bool,
    /// Whether to encrypt data before uploading
    pub encrypt_before_upload: bool,
    /// Conflict resolution strategy
    pub conflict_resolution: ConflictResolution,
}

impl Default for CloudSyncConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            provider: CloudProvider::Local,
            provider_config: ProviderSpecificConfig::Local(LocalConfig::default()),
            sync_frequency_seconds: 300, // 5 minutes
            auto_sync: true,
            encrypt_before_upload: true,
            conflict_resolution: ConflictResolution::LastWriteWins,
        }
    }
}

/// Conflict resolution strategies
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConflictResolution {
    /// Last write wins (by timestamp)
    LastWriteWins,
    /// Local version wins
    LocalWins,
    /// Remote version wins
    RemoteWins,
    /// Keep both versions
    KeepBoth,
    /// Manual resolution required
    Manual,
}

/// Provider-specific configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum ProviderSpecificConfig {
    Local(LocalConfig),
    S3(S3Config),
    AzureBlob(AzureBlobConfig),
    Gcs(GcsConfig),
    Dropbox(DropboxConfig),
    ICloud(ICloudConfig),
    OneDrive(OneDriveConfig),
    WebDav(WebDavConfig),
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct LocalConfig {
    pub sync_directory: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct S3Config {
    pub bucket: String,
    pub region: String,
    pub prefix: Option<String>,
    pub access_key_id: Option<String>,
    pub secret_access_key: Option<String>,
    pub endpoint: Option<String>, // For S3-compatible services
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AzureBlobConfig {
    pub container: String,
    pub connection_string: Option<String>,
    pub account_name: Option<String>,
    pub account_key: Option<String>,
    pub prefix: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GcsConfig {
    pub bucket: String,
    pub project_id: String,
    pub prefix: Option<String>,
    pub credentials_file: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DropboxConfig {
    pub access_token: Option<String>,
    pub refresh_token: Option<String>,
    pub folder_path: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ICloudConfig {
    pub container_id: Option<String>,
    pub folder_path: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OneDriveConfig {
    pub access_token: Option<String>,
    pub refresh_token: Option<String>,
    pub folder_path: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebDavConfig {
    pub url: String,
    pub username: Option<String>,
    pub password: Option<String>,
    pub folder_path: Option<String>,
}

// =============================================================================
// Sync State Tracking
// =============================================================================

/// Sync state for a single session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionSyncState {
    /// Session ID
    pub session_id: String,
    /// Local modification timestamp
    pub local_modified: i64,
    /// Remote modification timestamp (if known)
    pub remote_modified: Option<i64>,
    /// Local content hash
    pub local_hash: String,
    /// Remote content hash (if known)
    pub remote_hash: Option<String>,
    /// Sync status
    pub status: SyncStatus,
    /// Last sync attempt timestamp
    pub last_sync_attempt: Option<i64>,
    /// Last successful sync timestamp
    pub last_sync_success: Option<i64>,
    /// Error message from last failed sync
    pub last_error: Option<String>,
}

/// Sync status for a session
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SyncStatus {
    /// In sync with remote
    Synced,
    /// Local changes pending upload
    PendingUpload,
    /// Remote changes pending download
    PendingDownload,
    /// Conflict detected
    Conflict,
    /// Currently syncing
    Syncing,
    /// Sync error
    Error,
    /// Never synced
    NeverSynced,
}

/// Overall sync state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncState {
    /// Last full sync timestamp
    pub last_full_sync: Option<i64>,
    /// Per-session sync states
    pub sessions: Vec<SessionSyncState>,
    /// Pending operations count
    pub pending_uploads: u32,
    pub pending_downloads: u32,
    pub conflicts: u32,
}

impl SyncState {
    pub fn new() -> Self {
        Self {
            last_full_sync: None,
            sessions: Vec::new(),
            pending_uploads: 0,
            pending_downloads: 0,
            conflicts: 0,
        }
    }
}

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

// =============================================================================
// Cloud Sync Service
// =============================================================================

/// Cloud sync service trait
#[async_trait::async_trait]
pub trait CloudSyncService: Send + Sync {
    /// Get provider type
    fn provider(&self) -> CloudProvider;

    /// Test connection
    async fn test_connection(&self) -> Result<bool>;

    /// List remote sessions
    async fn list_remote_sessions(&self) -> Result<Vec<RemoteSessionInfo>>;

    /// Upload a session
    async fn upload_session(&self, session_id: &str, data: &[u8]) -> Result<UploadResult>;

    /// Download a session
    async fn download_session(&self, session_id: &str) -> Result<Vec<u8>>;

    /// Delete a remote session
    async fn delete_remote_session(&self, session_id: &str) -> Result<()>;

    /// Get remote session metadata
    async fn get_remote_metadata(&self, session_id: &str) -> Result<Option<RemoteSessionInfo>>;
}

/// Remote session information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemoteSessionInfo {
    pub session_id: String,
    pub modified_at: i64,
    pub size_bytes: u64,
    pub content_hash: String,
    pub metadata: Option<serde_json::Value>,
}

/// Upload result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UploadResult {
    pub success: bool,
    pub remote_path: String,
    pub content_hash: String,
    pub uploaded_at: i64,
}

// =============================================================================
// Local Sync Implementation (File-based)
// =============================================================================

/// Local file-based sync (for network drives, etc.)
pub struct LocalSyncService {
    sync_dir: PathBuf,
}

impl LocalSyncService {
    pub fn new(sync_dir: PathBuf) -> Self {
        Self { sync_dir }
    }

    fn session_path(&self, session_id: &str) -> PathBuf {
        self.sync_dir.join(format!("{}.json", session_id))
    }
}

#[async_trait::async_trait]
impl CloudSyncService for LocalSyncService {
    fn provider(&self) -> CloudProvider {
        CloudProvider::Local
    }

    async fn test_connection(&self) -> Result<bool> {
        Ok(self.sync_dir.exists() || std::fs::create_dir_all(&self.sync_dir).is_ok())
    }

    async fn list_remote_sessions(&self) -> Result<Vec<RemoteSessionInfo>> {
        let mut sessions = Vec::new();

        if !self.sync_dir.exists() {
            return Ok(sessions);
        }

        for entry in std::fs::read_dir(&self.sync_dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.extension().and_then(|s| s.to_str()) == Some("json") {
                if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
                    let metadata = entry.metadata()?;
                    let modified = metadata
                        .modified()?
                        .duration_since(UNIX_EPOCH)
                        .unwrap_or_default()
                        .as_secs() as i64;

                    // Simple hash based on size and modified time
                    let hash = format!("{}-{}", metadata.len(), modified);

                    sessions.push(RemoteSessionInfo {
                        session_id: stem.to_string(),
                        modified_at: modified,
                        size_bytes: metadata.len(),
                        content_hash: hash,
                        metadata: None,
                    });
                }
            }
        }

        Ok(sessions)
    }

    async fn upload_session(&self, session_id: &str, data: &[u8]) -> Result<UploadResult> {
        std::fs::create_dir_all(&self.sync_dir)?;

        let path = self.session_path(session_id);
        std::fs::write(&path, data)?;

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs() as i64;

        // Simple hash
        let hash = format!("{}-{}", data.len(), now);

        Ok(UploadResult {
            success: true,
            remote_path: path.to_string_lossy().to_string(),
            content_hash: hash,
            uploaded_at: now,
        })
    }

    async fn download_session(&self, session_id: &str) -> Result<Vec<u8>> {
        let path = self.session_path(session_id);
        std::fs::read(&path).map_err(|e| anyhow!("Failed to read session: {}", e))
    }

    async fn delete_remote_session(&self, session_id: &str) -> Result<()> {
        let path = self.session_path(session_id);
        if path.exists() {
            std::fs::remove_file(&path)?;
        }
        Ok(())
    }

    async fn get_remote_metadata(&self, session_id: &str) -> Result<Option<RemoteSessionInfo>> {
        let path = self.session_path(session_id);

        if !path.exists() {
            return Ok(None);
        }

        let metadata = std::fs::metadata(&path)?;
        let modified = metadata
            .modified()?
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs() as i64;

        let hash = format!("{}-{}", metadata.len(), modified);

        Ok(Some(RemoteSessionInfo {
            session_id: session_id.to_string(),
            modified_at: modified,
            size_bytes: metadata.len(),
            content_hash: hash,
            metadata: None,
        }))
    }
}

// =============================================================================
// Sync Manager
// =============================================================================

/// Main sync manager that coordinates synchronization
pub struct SyncManager {
    config: CloudSyncConfig,
    state: SyncState,
    service: Option<Box<dyn CloudSyncService>>,
}

impl SyncManager {
    pub fn new(config: CloudSyncConfig) -> Self {
        Self {
            config,
            state: SyncState::new(),
            service: None,
        }
    }

    /// Initialize the sync service based on configuration
    pub fn initialize(&mut self) -> Result<()> {
        if !self.config.enabled {
            return Ok(());
        }

        match &self.config.provider_config {
            ProviderSpecificConfig::Local(local_config) => {
                let sync_dir = local_config
                    .sync_directory
                    .as_ref()
                    .map(PathBuf::from)
                    .unwrap_or_else(|| {
                        dirs::data_local_dir()
                            .unwrap_or_else(|| PathBuf::from("."))
                            .join("csm")
                            .join("sync")
                    });
                self.service = Some(Box::new(LocalSyncService::new(sync_dir)));
            }
            _ => {
                return Err(anyhow!(
                    "Cloud provider {:?} not yet implemented",
                    self.config.provider
                ));
            }
        }

        Ok(())
    }

    /// Test connection to cloud service
    pub async fn test_connection(&self) -> Result<bool> {
        match &self.service {
            Some(service) => service.test_connection().await,
            None => Err(anyhow!("Sync service not initialized")),
        }
    }

    /// Get current sync state
    pub fn get_state(&self) -> &SyncState {
        &self.state
    }

    /// Sync all sessions
    pub async fn sync_all(&mut self) -> Result<SyncResult> {
        let service = self
            .service
            .as_ref()
            .ok_or_else(|| anyhow!("Sync service not initialized"))?;

        let result = SyncResult {
            uploaded: 0,
            downloaded: 0,
            conflicts: 0,
            errors: Vec::new(),
        };

        // Get remote sessions
        let _remote_sessions = service.list_remote_sessions().await?;

        // Update state
        self.state.last_full_sync = Some(
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs() as i64,
        );

        // TODO: Compare local and remote, perform sync operations

        Ok(result)
    }

    /// Upload a specific session
    pub async fn upload_session(&mut self, session_id: &str, data: &[u8]) -> Result<UploadResult> {
        let service = self
            .service
            .as_ref()
            .ok_or_else(|| anyhow!("Sync service not initialized"))?;

        service.upload_session(session_id, data).await
    }

    /// Download a specific session
    pub async fn download_session(&self, session_id: &str) -> Result<Vec<u8>> {
        let service = self
            .service
            .as_ref()
            .ok_or_else(|| anyhow!("Sync service not initialized"))?;

        service.download_session(session_id).await
    }
}

/// Result of a sync operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncResult {
    pub uploaded: u32,
    pub downloaded: u32,
    pub conflicts: u32,
    pub errors: Vec<String>,
}

// =============================================================================
// Tests
// =============================================================================

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

    #[tokio::test]
    async fn test_local_sync_service() {
        let temp_dir = tempdir().unwrap();
        let sync_dir = temp_dir.path().join("sync");

        let service = LocalSyncService::new(sync_dir.clone());

        // Test connection
        assert!(service.test_connection().await.unwrap());

        // Test upload
        let data = b"test session data";
        let result = service.upload_session("test-session", data).await.unwrap();
        assert!(result.success);

        // Test list
        let sessions = service.list_remote_sessions().await.unwrap();
        assert_eq!(sessions.len(), 1);
        assert_eq!(sessions[0].session_id, "test-session");

        // Test download
        let downloaded = service.download_session("test-session").await.unwrap();
        assert_eq!(downloaded, data);

        // Test delete
        service.delete_remote_session("test-session").await.unwrap();
        let sessions = service.list_remote_sessions().await.unwrap();
        assert!(sessions.is_empty());
    }
}