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
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! Workspace discovery and management

use crate::error::{CsmError, Result};
use crate::models::{SessionWithPath, Workspace, WorkspaceJson};
use crate::storage::{is_session_file_extension, parse_session_file};
use std::path::{Path, PathBuf};
use urlencoding::decode;

/// Type alias for workspace info tuple (hash, path, project_path, modified_time)
pub type WorkspaceInfo = (String, PathBuf, Option<String>, std::time::SystemTime);

/// Get the VS Code workspaceStorage path based on the operating system
pub fn get_workspace_storage_path() -> Result<PathBuf> {
    let path = if cfg!(target_os = "windows") {
        dirs::config_dir().map(|p| p.join("Code").join("User").join("workspaceStorage"))
    } else if cfg!(target_os = "macos") {
        dirs::home_dir().map(|p| p.join("Library/Application Support/Code/User/workspaceStorage"))
    } else {
        // Linux
        dirs::home_dir().map(|p| p.join(".config/Code/User/workspaceStorage"))
    };

    path.ok_or(CsmError::StorageNotFound)
}

/// Get the VS Code globalStorage path based on the operating system
pub fn get_global_storage_path() -> Result<PathBuf> {
    let path = if cfg!(target_os = "windows") {
        dirs::config_dir().map(|p| p.join("Code").join("User").join("globalStorage"))
    } else if cfg!(target_os = "macos") {
        dirs::home_dir().map(|p| p.join("Library/Application Support/Code/User/globalStorage"))
    } else {
        // Linux
        dirs::home_dir().map(|p| p.join(".config/Code/User/globalStorage"))
    };

    path.ok_or(CsmError::StorageNotFound)
}

/// Get the path to empty window chat sessions (ALL SESSIONS in VS Code)
/// These are chat sessions not tied to any specific workspace
pub fn get_empty_window_sessions_path() -> Result<PathBuf> {
    let global_storage = get_global_storage_path()?;
    Ok(global_storage.join("emptyWindowChatSessions"))
}

/// Decode a workspace folder URI to a path
pub fn decode_workspace_folder(folder_uri: &str) -> String {
    let mut folder = folder_uri.to_string();

    // Remove file:// prefix
    if folder.starts_with("file:///") {
        folder = folder[8..].to_string();
    } else if folder.starts_with("file://") {
        folder = folder[7..].to_string();
    }

    // URL decode
    if let Ok(decoded) = decode(&folder) {
        folder = decoded.into_owned();
    }

    // On Windows, convert forward slashes to backslashes
    if cfg!(target_os = "windows") {
        folder = folder.replace('/', "\\");
    }

    folder
}

/// Normalize a path for comparison
pub fn normalize_path(path: &str) -> String {
    let path = Path::new(path);
    if let Ok(canonical) = path.canonicalize() {
        canonical.to_string_lossy().to_lowercase()
    } else {
        // Fallback: lowercase and strip trailing slashes
        let normalized = path.to_string_lossy().to_lowercase();
        normalized.trim_end_matches(['/', '\\']).to_string()
    }
}

/// Discover all VS Code workspaces
pub fn discover_workspaces() -> Result<Vec<Workspace>> {
    let storage_path = get_workspace_storage_path()?;

    if !storage_path.exists() {
        return Ok(Vec::new());
    }

    let mut workspaces = Vec::new();

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

        if !workspace_dir.is_dir() {
            continue;
        }

        let workspace_json_path = workspace_dir.join("workspace.json");
        if !workspace_json_path.exists() {
            continue;
        }

        // Parse workspace.json
        let project_path = match std::fs::read_to_string(&workspace_json_path) {
            Ok(content) => match serde_json::from_str::<WorkspaceJson>(&content) {
                Ok(ws_json) => ws_json.folder.map(|f| decode_workspace_folder(&f)),
                Err(_) => None,
            },
            Err(_) => None,
        };

        let chat_sessions_path = workspace_dir.join("chatSessions");
        let has_chat_sessions = chat_sessions_path.exists();

        let chat_session_count = if has_chat_sessions {
            std::fs::read_dir(&chat_sessions_path)
                .map(|entries| {
                    entries
                        .filter_map(|e| e.ok())
                        .filter(|e| {
                            e.path()
                                .extension()
                                .map(is_session_file_extension)
                                .unwrap_or(false)
                        })
                        .count()
                })
                .unwrap_or(0)
        } else {
            0
        };

        // Get last modified time
        let last_modified = if has_chat_sessions {
            std::fs::read_dir(&chat_sessions_path)
                .ok()
                .and_then(|entries| {
                    entries
                        .filter_map(|e| e.ok())
                        .filter_map(|e| e.metadata().ok())
                        .filter_map(|m| m.modified().ok())
                        .max()
                })
                .map(chrono::DateTime::<Utc>::from)
        } else {
            None
        };

        workspaces.push(Workspace {
            hash: entry.file_name().to_string_lossy().to_string(),
            project_path,
            workspace_path: workspace_dir.clone(),
            chat_sessions_path,
            chat_session_count,
            has_chat_sessions,
            last_modified,
        });
    }

    Ok(workspaces)
}

/// Find a workspace by its hash
pub fn get_workspace_by_hash(hash: &str) -> Result<Option<Workspace>> {
    let workspaces = discover_workspaces()?;
    Ok(workspaces
        .into_iter()
        .find(|w| w.hash == hash || w.hash.starts_with(hash)))
}

/// Find a workspace by project path
pub fn get_workspace_by_path(project_path: &str) -> Result<Option<Workspace>> {
    let workspaces = discover_workspaces()?;
    let target_path = normalize_path(project_path);

    Ok(workspaces.into_iter().find(|w| {
        w.project_path
            .as_ref()
            .map(|p| normalize_path(p) == target_path)
            .unwrap_or(false)
    }))
}

/// Find workspace by path, returning workspace ID, directory, and data.
/// When multiple workspaces match the same path, returns the most recently modified one.
pub fn find_workspace_by_path(
    project_path: &str,
) -> Result<Option<(String, PathBuf, Option<String>)>> {
    let storage_path = get_workspace_storage_path()?;

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

    let target_path = normalize_path(project_path);
    let mut matches: Vec<(String, PathBuf, Option<String>, std::time::SystemTime)> = Vec::new();

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

        if !workspace_dir.is_dir() {
            continue;
        }

        let workspace_json_path = workspace_dir.join("workspace.json");
        if !workspace_json_path.exists() {
            continue;
        }

        if let Ok(content) = std::fs::read_to_string(&workspace_json_path) {
            if let Ok(ws_json) = serde_json::from_str::<WorkspaceJson>(&content) {
                if let Some(folder) = &ws_json.folder {
                    let folder_path = decode_workspace_folder(folder);
                    if normalize_path(&folder_path) == target_path {
                        // Get the most recent modification time from chatSessions or workspace dir
                        let chat_sessions_dir = workspace_dir.join("chatSessions");
                        let last_modified = if chat_sessions_dir.exists() {
                            std::fs::read_dir(&chat_sessions_dir)
                                .ok()
                                .and_then(|entries| {
                                    entries
                                        .filter_map(|e| e.ok())
                                        .filter_map(|e| e.metadata().ok())
                                        .filter_map(|m| m.modified().ok())
                                        .max()
                                })
                                .unwrap_or_else(|| {
                                    chat_sessions_dir
                                        .metadata()
                                        .and_then(|m| m.modified())
                                        .unwrap_or(std::time::UNIX_EPOCH)
                                })
                        } else {
                            workspace_dir
                                .metadata()
                                .and_then(|m| m.modified())
                                .unwrap_or(std::time::UNIX_EPOCH)
                        };

                        matches.push((
                            entry.file_name().to_string_lossy().to_string(),
                            workspace_dir,
                            Some(folder_path),
                            last_modified,
                        ));
                    }
                }
            }
        }
    }

    // Sort by last modified (newest first) and return the most recent
    matches.sort_by(|a, b| b.3.cmp(&a.3));

    Ok(matches
        .into_iter()
        .next()
        .map(|(id, path, folder, _)| (id, path, folder)))
}

/// Find all workspaces for a project (by name matching)
pub fn find_all_workspaces_for_project(project_name: &str) -> Result<Vec<WorkspaceInfo>> {
    let storage_path = get_workspace_storage_path()?;

    if !storage_path.exists() {
        return Ok(Vec::new());
    }

    let project_name_lower = project_name.to_lowercase();
    let mut workspaces = Vec::new();

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

        if !workspace_dir.is_dir() {
            continue;
        }

        let workspace_json_path = workspace_dir.join("workspace.json");
        if !workspace_json_path.exists() {
            continue;
        }

        if let Ok(content) = std::fs::read_to_string(&workspace_json_path) {
            if let Ok(ws_json) = serde_json::from_str::<WorkspaceJson>(&content) {
                if let Some(folder) = &ws_json.folder {
                    let folder_path = decode_workspace_folder(folder);

                    if folder_path.to_lowercase().contains(&project_name_lower) {
                        let chat_sessions_dir = workspace_dir.join("chatSessions");

                        let last_modified = if chat_sessions_dir.exists() {
                            std::fs::read_dir(&chat_sessions_dir)
                                .ok()
                                .and_then(|entries| {
                                    entries
                                        .filter_map(|e| e.ok())
                                        .filter_map(|e| e.metadata().ok())
                                        .filter_map(|m| m.modified().ok())
                                        .max()
                                })
                                .unwrap_or_else(|| {
                                    chat_sessions_dir
                                        .metadata()
                                        .and_then(|m| m.modified())
                                        .unwrap_or(std::time::UNIX_EPOCH)
                                })
                        } else {
                            workspace_dir
                                .metadata()
                                .and_then(|m| m.modified())
                                .unwrap_or(std::time::UNIX_EPOCH)
                        };

                        workspaces.push((
                            entry.file_name().to_string_lossy().to_string(),
                            workspace_dir,
                            Some(folder_path),
                            last_modified,
                        ));
                    }
                }
            }
        }
    }

    // Sort by last modified (newest first)
    workspaces.sort_by(|a, b| b.3.cmp(&a.3));

    Ok(workspaces)
}

/// Get all chat sessions from a workspace directory
pub fn get_chat_sessions_from_workspace(workspace_dir: &Path) -> Result<Vec<SessionWithPath>> {
    let chat_sessions_dir = workspace_dir.join("chatSessions");

    if !chat_sessions_dir.exists() {
        return Ok(Vec::new());
    }

    let mut sessions = Vec::new();

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

        if path
            .extension()
            .map(is_session_file_extension)
            .unwrap_or(false)
        {
            if let Ok(session) = parse_session_file(&path) {
                sessions.push(SessionWithPath { path, session });
            }
        }
    }

    // Deduplicate by session_id: when a .jsonl has 0 requests but a .backup exists
    // with real data, prefer the .backup. This handles VS Code migrations that left
    // empty .jsonl files alongside .backup files with the actual session content.
    deduplicate_sessions(&mut sessions);

    Ok(sessions)
}

/// Deduplicate sessions by session ID, keeping the version with more requests.
/// This handles the case where VS Code created an empty .jsonl file during
/// migration but the real data remains in a .backup file.
fn deduplicate_sessions(sessions: &mut Vec<SessionWithPath>) {
    use std::collections::HashMap;

    if sessions.len() <= 1 {
        return;
    }

    // Group by session_id
    let mut best: HashMap<String, usize> = HashMap::new();
    let mut to_remove = Vec::new();

    for (i, swp) in sessions.iter().enumerate() {
        let sid = swp.session.session_id.clone().unwrap_or_default();
        if sid.is_empty() {
            continue;
        }

        if let Some(&prev_idx) = best.get(&sid) {
            let prev_count = sessions[prev_idx].session.request_count();
            let curr_count = swp.session.request_count();
            if curr_count > prev_count {
                to_remove.push(prev_idx);
                best.insert(sid, i);
            } else {
                to_remove.push(i);
            }
        } else {
            best.insert(sid, i);
        }
    }

    // Remove duplicates in reverse order to preserve indices
    to_remove.sort_unstable();
    to_remove.dedup();
    for idx in to_remove.into_iter().rev() {
        sessions.remove(idx);
    }
}

/// Recover orphaned sessions from old workspace hashes into the active workspace.
///
/// When a project folder is opened multiple times in VS Code (or when the path
/// changes slightly), multiple workspace hashes can accumulate. Sessions in
/// older hashes become invisible. This function:
/// 1. Finds ALL workspace hashes whose `workspace.json` folder matches `project_path`
/// 2. Identifies the most-recently-modified hash as "active"
/// 3. Copies `.json`, `.jsonl`, and `.backup` session files from orphaned hashes
///    to the active hash's `chatSessions/` directory (skipping duplicates)
///
/// Returns the number of session files recovered.
pub fn recover_orphaned_sessions_from_old_hashes(project_path: &str) -> Result<usize> {
    let storage_path = get_workspace_storage_path()?;
    if !storage_path.exists() {
        return Ok(0);
    }

    let target_path = normalize_path(project_path);

    // Find all workspace hashes that match this path (exact normalized match)
    let mut all_matches: Vec<(String, PathBuf, std::time::SystemTime)> = Vec::new();

    for entry in std::fs::read_dir(&storage_path)? {
        let entry = entry?;
        let workspace_dir = entry.path();
        if !workspace_dir.is_dir() {
            continue;
        }

        let ws_json_path = workspace_dir.join("workspace.json");
        if !ws_json_path.exists() {
            continue;
        }

        if let Ok(content) = std::fs::read_to_string(&ws_json_path) {
            if let Ok(ws_json) = serde_json::from_str::<WorkspaceJson>(&content) {
                if let Some(folder) = &ws_json.folder {
                    let folder_path = decode_workspace_folder(folder);
                    if normalize_path(&folder_path) == target_path {
                        let chat_dir = workspace_dir.join("chatSessions");
                        let last_modified = if chat_dir.exists() {
                            std::fs::read_dir(&chat_dir)
                                .ok()
                                .and_then(|entries| {
                                    entries
                                        .filter_map(|e| e.ok())
                                        .filter_map(|e| e.metadata().ok())
                                        .filter_map(|m| m.modified().ok())
                                        .max()
                                })
                                .unwrap_or(std::time::UNIX_EPOCH)
                        } else {
                            std::time::UNIX_EPOCH
                        };

                        all_matches.push((
                            entry.file_name().to_string_lossy().to_string(),
                            workspace_dir,
                            last_modified,
                        ));
                    }
                }
            }
        }
    }

    if all_matches.len() <= 1 {
        return Ok(0); // No orphaned hashes
    }

    // Sort by last modified (newest first); first = active
    all_matches.sort_by(|a, b| b.2.cmp(&a.2));

    let active_dir = all_matches[0].1.join("chatSessions");
    if !active_dir.exists() {
        std::fs::create_dir_all(&active_dir)?;
    }

    // Collect existing session stems in the active workspace
    let mut existing_stems: std::collections::HashSet<String> = std::collections::HashSet::new();
    if let Ok(entries) = std::fs::read_dir(&active_dir) {
        for entry in entries.flatten() {
            if let Some(stem) = entry.path().file_stem() {
                existing_stems.insert(stem.to_string_lossy().to_string());
            }
        }
    }

    let mut recovered = 0;

    // Copy from orphaned hashes (index 1..n)
    for (_hash, orphan_dir, _) in &all_matches[1..] {
        let orphan_sessions = orphan_dir.join("chatSessions");
        if !orphan_sessions.exists() {
            continue;
        }

        if let Ok(entries) = std::fs::read_dir(&orphan_sessions) {
            for entry in entries.flatten() {
                let src = entry.path();
                let ext_match = src
                    .extension()
                    .map(|e| e == "json" || e == "jsonl" || e == "backup")
                    .unwrap_or(false);
                // Skip backup copies (.bak, .corrupt)
                let is_bak = src.to_string_lossy().ends_with(".bak")
                    || src.to_string_lossy().ends_with(".corrupt");

                if ext_match && !is_bak {
                    let stem = src
                        .file_stem()
                        .map(|s| s.to_string_lossy().to_string())
                        .unwrap_or_default();

                    // Only copy if this session doesn't already exist in the active workspace
                    if !existing_stems.contains(&stem) {
                        let filename = src.file_name().unwrap();
                        let dest = active_dir.join(filename);
                        if !dest.exists() {
                            std::fs::copy(&src, &dest)?;
                            existing_stems.insert(stem);
                            recovered += 1;
                        }
                    }
                }
            }
        }
    }

    Ok(recovered)
}

use chrono::Utc;