-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathworkspaceHelpers.ts
More file actions
712 lines (631 loc) · 25.6 KB
/
workspaceHelpers.ts
File metadata and controls
712 lines (631 loc) · 25.6 KB
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/**
* Workspace resolution and utility helper functions.
* Pure functions extracted from CopilotTokenTracker.
*/
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import type { CustomizationFileEntry } from './types';
import * as packageJson from '../package.json';
import customizationPatternsData from './customizationPatterns.json';
/**
* Resolve the workspace folder full path from a session file path.
* Looks for a `workspaceStorage/<id>/` segment and reads `workspace.json` or `meta.json`.
* Synchronous by design to keep the analysis flow simple and cached.
*/
// Helper: read a workspaceStorage JSON file and extract a candidate folder path from configured keys
export function parseWorkspaceStorageJsonFile(jsonPath: string, candidateKeys: string[]): string | undefined {
try {
const raw = fs.readFileSync(jsonPath, 'utf8');
const obj = JSON.parse(raw);
for (const key of candidateKeys) {
const candidate = obj[key];
if (typeof candidate !== 'string') { continue; }
const pathCandidate = candidate.replace(/^file:\/\//, '');
// Prefer vscode.Uri.parse -> fsPath when possible
try {
const uri = vscode.Uri.parse(candidate);
if (uri.fsPath && uri.fsPath.length > 0) {
return uri.fsPath;
}
} catch { }
try {
return decodeURIComponent(pathCandidate);
} catch {
return pathCandidate;
}
}
} catch {
// ignore parse/read errors
}
return undefined;
}
/**
* Extract workspace ID from a session file path, if it's workspace-scoped.
* Returns the workspace ID or undefined if not a workspace-scoped session.
*/
export function extractWorkspaceIdFromSessionPath(sessionFilePath: string): string | undefined {
try {
const normalized = sessionFilePath.replace(/\\/g, '/');
const parts = normalized.split('/').filter(p => p.length > 0);
const idx = parts.findIndex(p => p.toLowerCase() === 'workspacestorage');
if (idx === -1 || idx + 1 >= parts.length) {
return undefined; // Not a workspace-scoped session file
}
return parts[idx + 1];
} catch {
return undefined;
}
}
/**
* Convert a simple glob pattern to a RegExp.
* Supports: ** (match multiple path segments), * (match within a segment), ?.
*/
export function globToRegExp(glob: string, caseInsensitive: boolean = false): RegExp {
// Normalize to posix-style
let pattern = glob.replace(/\\/g, '/');
// Escape regex special chars
pattern = pattern.replace(/([.+^=!:${}()|[\]\\])/g, '\\$1');
// Replace /**/ or ** with placeholder
pattern = pattern.replace(/(^|\/)\*\*\/(?!$)/g, '$1__GLOBSTAR__/');
pattern = pattern.replace(/\*\*/g, '__GLOBSTAR__');
// Replace single * with [^/]* and ? with .
pattern = pattern.replace(/\*/g, '[^/]*').replace(/\?/g, '.');
// Replace globstar placeholder with .* (allow path separators)
pattern = pattern.replace(/__GLOBSTAR__\//g, '(?:.*?/?)').replace(/__GLOBSTAR__/g, '.*');
// Anchor
const flags = caseInsensitive ? 'i' : '';
return new RegExp('^' + pattern + '$', flags);
}
/**
* Resolve an exact relative path in a workspace.
* When `caseInsensitive` is true, path segments are matched case-insensitively.
*/
export function resolveExactWorkspacePath(workspaceFolderPath: string, relativePattern: string, caseInsensitive: boolean): string | undefined {
const directPath = path.join(workspaceFolderPath, relativePattern);
if (!caseInsensitive) {
return fs.existsSync(directPath) ? directPath : undefined;
}
if (fs.existsSync(directPath)) {
return directPath;
}
const normalized = relativePattern.replace(/\\/g, '/');
const segments = normalized.split('/').filter(seg => seg.length > 0 && seg !== '.');
let current = workspaceFolderPath;
for (let index = 0; index < segments.length; index++) {
const segment = segments[index];
const isLast = index === segments.length - 1;
if (!fs.existsSync(current)) {
return undefined;
}
let entries: fs.Dirent[] = [];
try {
entries = fs.readdirSync(current, { withFileTypes: true });
} catch {
return undefined;
}
const matchedEntry = entries.find(entry => entry.name.toLowerCase() === segment.toLowerCase());
if (!matchedEntry) {
return undefined;
}
const matchedPath = path.join(current, matchedEntry.name);
if (!isLast) {
let stat: fs.Stats;
try {
stat = fs.statSync(matchedPath);
} catch {
return undefined;
}
if (!stat.isDirectory()) {
return undefined;
}
}
current = matchedPath;
}
return fs.existsSync(current) ? current : undefined;
}
/**
* Scan a workspace folder for customization files according to `customizationPatterns.json`.
*/
export function scanWorkspaceCustomizationFiles(workspaceFolderPath: string): CustomizationFileEntry[] {
const results: CustomizationFileEntry[] = [];
if (!workspaceFolderPath || !fs.existsSync(workspaceFolderPath)) { return results; }
const cfg = customizationPatternsData as any;
const stalenessDays = typeof cfg.stalenessThresholdDays === 'number' ? cfg.stalenessThresholdDays : 90;
const excludeDirs: string[] = Array.isArray(cfg.excludeDirs) ? cfg.excludeDirs : [];
for (const pattern of (cfg.patterns || [])) {
try {
const scanMode = pattern.scanMode || 'exact';
const relativePattern = pattern.path as string;
if (scanMode === 'exact') {
const caseInsensitive = !!pattern.caseInsensitive;
const absPath = resolveExactWorkspacePath(workspaceFolderPath, relativePattern, caseInsensitive);
if (absPath) {
const stat = fs.statSync(absPath);
results.push({
path: absPath,
relativePath: path.relative(workspaceFolderPath, absPath).replace(/\\/g, '/'),
type: pattern.type || 'unknown',
icon: pattern.icon || '',
label: pattern.label || path.basename(absPath),
name: path.basename(absPath),
lastModified: stat.mtime.toISOString(),
isStale: (Date.now() - stat.mtime.getTime()) > stalenessDays * 24 * 60 * 60 * 1000,
category: pattern.category as 'copilot' | 'non-copilot' | undefined
});
}
} else if (scanMode === 'oneLevel') {
// Split at the first '*' wildcard to find base directory and remaining path
// e.g., ".github/skills/*/SKILL.md" -> base: ".github/skills/", remaining: "/SKILL.md"
const normalizedPattern = relativePattern.replace(/\\/g, '/');
const starIndex = normalizedPattern.indexOf('*');
if (starIndex === -1) { continue; } // No wildcard, skip
// Split the pattern at the '*'
const beforeStar = normalizedPattern.substring(0, starIndex);
const afterStar = normalizedPattern.substring(starIndex + 1);
// The base directory is everything before the '*' (trim trailing slash)
const baseDirPath = beforeStar.replace(/\/$/, '');
const baseDir = baseDirPath ? path.join(workspaceFolderPath, baseDirPath) : workspaceFolderPath;
if (!fs.existsSync(baseDir)) { continue; }
const baseStat = fs.statSync(baseDir);
if (!baseStat.isDirectory()) { continue; }
// Enumerate directories in the base directory
const entries = fs.readdirSync(baseDir, { withFileTypes: true });
const fullPattern = afterStar.startsWith('/') ? afterStar.substring(1) : afterStar;
for (const entry of entries) {
// Only consider directories at this level (unless afterStar is just a filename)
if (excludeDirs.includes(entry.name)) { continue; }
// Construct the full path with this entry replacing the '*'
const candidatePath = path.join(baseDir, entry.name, fullPattern);
// Check if this path exists
if (fs.existsSync(candidatePath)) {
const stat = fs.statSync(candidatePath);
if (stat.isFile()) {
// For skills, use the directory name (parent of SKILL.md) as the display name
const displayName = pattern.type === 'skill' ? entry.name : path.basename(candidatePath);
results.push({
path: candidatePath,
relativePath: path.relative(workspaceFolderPath, candidatePath).replace(/\\/g, '/'),
type: pattern.type || 'unknown',
icon: pattern.icon || '',
label: pattern.label || displayName,
name: displayName,
lastModified: stat.mtime.toISOString(),
category: pattern.category as 'copilot' | 'non-copilot' | undefined,
isStale: (Date.now() - stat.mtime.getTime()) > stalenessDays * 24 * 60 * 60 * 1000
});
}
}
}
} else if (scanMode === 'recursive') {
const maxDepth = typeof pattern.maxDepth === 'number' ? pattern.maxDepth : 6;
const caseInsensitive = !!pattern.caseInsensitive;
const regex = globToRegExp(relativePattern, caseInsensitive);
// Walk recursively
const walk = (dir: string, depth: number) => {
if (depth < 0) { return; }
let children: fs.Dirent[] = [];
try { children = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; }
for (const child of children) {
const name = child.name;
if (child.isDirectory()) {
if (excludeDirs.includes(name)) { continue; }
walk(path.join(dir, name), depth - 1);
} else if (child.isFile()) {
const rel = path.relative(workspaceFolderPath, path.join(dir, name)).replace(/\\/g, '/');
if (regex.test(rel)) {
const abs = path.join(dir, name);
const stat = fs.statSync(abs);
results.push({
path: abs,
relativePath: rel,
type: pattern.type || 'unknown',
icon: pattern.icon || '',
label: pattern.label || path.basename(abs),
name: path.basename(abs),
lastModified: stat.mtime.toISOString(),
isStale: (Date.now() - stat.mtime.getTime()) > stalenessDays * 24 * 60 * 60 * 1000,
category: pattern.category as 'copilot' | 'non-copilot' | undefined
});
}
}
}
};
walk(workspaceFolderPath, maxDepth);
}
} catch (e) {
// ignore per-pattern errors
}
}
// Deduplicate by absolute path
const uniq: { [p: string]: CustomizationFileEntry } = {};
for (const r of results) { uniq[path.normalize(r.path)] = r; }
return Object.values(uniq);
}
// Helper method to get repository URL from package.json
export function getRepositoryUrl(): string {
const repoUrl = packageJson.repository?.url?.replace(/^git\+/, '').replace(/\.git$/, '');
return repoUrl || 'https://github.com/rajbos/github-copilot-token-usage';
}
/**
* Determine the editor type from a session file path
* Returns: 'VS Code', 'VS Code Insiders', 'VSCodium', 'Cursor', 'Copilot CLI', or 'Unknown'
*/
/**
* Detect the actual mode type from inputState.mode object.
* Returns 'ask', 'edit', 'agent', 'plan', or 'customAgent'.
*/
export function getModeType(mode: any): 'ask' | 'edit' | 'agent' | 'plan' | 'customAgent' {
if (!mode || !mode.kind) {
return 'ask';
}
// Check kind first - edit and ask are straightforward
if (mode.kind === 'edit') { return 'edit'; }
if (mode.kind === 'ask') { return 'ask'; }
// For agent kind, check the mode.id to differentiate
if (mode.kind === 'agent') {
if (!mode.id || mode.id === 'agent') {
// Standard agent mode (no special id or id='agent')
return 'agent';
}
// Check for plan mode (vscode-userdata:/.../plan-agent/Plan.agent.md)
if (typeof mode.id === 'string' && mode.id.includes('plan-agent/Plan.agent.md')) {
return 'plan';
}
// Check for custom agent (file:// URI to .agent.md)
if (typeof mode.id === 'string' && mode.id.includes('.agent.md')) {
return 'customAgent';
}
// Fallback to standard agent for any other agent kind
return 'agent';
}
// Default to ask for unknown modes
return 'ask';
}
/**
* Extract custom agent name from a file:// URI pointing to a .agent.md file.
* Returns the filename without the .agent.md extension.
*/
export function extractCustomAgentName(modeId: string): string | null {
if (!modeId || !modeId.includes('.agent.md')) {
return null;
}
try {
// Handle both file:/// URIs and regular paths
const cleanPath = modeId.replace('file:///', '').replace('file://', '');
const decodedPath = decodeURIComponent(cleanPath);
const parts = decodedPath.split(/[\\/]/);
const filename = parts[parts.length - 1];
// Remove .agent.md extension
if (filename.endsWith('.agent.md')) {
return filename.slice(0, -10); // Remove '.agent.md'
}
if (filename.endsWith('.md.agent.md')) {
// Handle case like TestEngineerAgent.md.agent.md
return filename.slice(0, -10).replace('.md', '');
}
} catch (e) {
return null;
}
return null;
}
/**
* Determine a friendly editor name from an editor root path (folder name)
* e.g. 'C:\...\AppData\Roaming\Code' -> 'VS Code'
*/
export function getEditorNameFromRoot(rootPath: string): string {
if (!rootPath) { return 'Unknown'; }
const lower = rootPath.toLowerCase();
// Check obvious markers first
if (lower.includes('.copilot') || lower.includes('copilot')) { return 'Copilot CLI'; }
if (lower.includes('opencode')) { return 'OpenCode'; }
if (lower.includes('code - insiders') || lower.includes('code-insiders') || lower.includes('insiders')) { return 'VS Code Insiders'; }
if (lower.includes('code - exploration') || lower.includes('code%20-%20exploration')) { return 'VS Code Exploration'; }
if (lower.includes('vscodium')) { return 'VSCodium'; }
if (lower.includes('cursor')) { return 'Cursor'; }
// Generic 'code' match (catch AppData\Roaming\Code)
if (lower.endsWith('code') || lower.includes(path.sep + 'code' + path.sep) || lower.includes('/code/')) { return 'VS Code'; }
return 'Unknown';
}
/**
* Extract a friendly display name from a repository URL.
* Supports HTTPS, SSH, and git:// URLs.
* @param repoUrl The full repository URL
* @returns A shortened display name like "owner/repo"
*/
export function getRepoDisplayName(repoUrl: string): string {
if (!repoUrl || repoUrl === 'Unknown') { return 'Unknown'; }
// Remove .git suffix if present
let url = repoUrl.replace(/\.git$/, '');
// Handle SSH URLs like git@github.com:owner/repo
if (url.includes('@') && url.includes(':')) {
const colonIndex = url.lastIndexOf(':');
const atIndex = url.lastIndexOf('@');
if (colonIndex > atIndex) {
return url.substring(colonIndex + 1);
}
}
// Handle HTTPS/git URLs - extract path after the host
try {
if (url.includes('://')) {
const urlObj = new URL(url);
const pathParts = urlObj.pathname.split('/').filter(p => p);
if (pathParts.length >= 2) {
return `${pathParts[pathParts.length - 2]}/${pathParts[pathParts.length - 1]}`;
}
return urlObj.pathname.replace(/^\//, '');
}
} catch {
// URL parsing failed, continue to fallback
}
// Fallback: return the last part of the path
const parts = url.split('/').filter(p => p);
if (parts.length >= 2) {
return `${parts[parts.length - 2]}/${parts[parts.length - 1]}`;
}
return url;
}
/**
* Parse the remote origin URL from a .git/config file content.
* Looks for [remote "origin"] section and extracts the url value.
* @param gitConfigContent The content of a .git/config file
* @returns The remote origin URL if found, undefined otherwise
*/
export function parseGitRemoteUrl(gitConfigContent: string): string | undefined {
// Look for [remote "origin"] section and extract url
const lines = gitConfigContent.split('\n');
let inOriginSection = false;
for (const line of lines) {
const trimmed = line.trim();
// Check if we're entering the [remote "origin"] section
if (trimmed.match(/^\[remote\s+"origin"\]$/i)) {
inOriginSection = true;
continue;
}
// Check if we're leaving the section (new section starts)
if (inOriginSection && trimmed.startsWith('[')) {
inOriginSection = false;
continue;
}
// Look for url = ... in the origin section
if (inOriginSection) {
const urlMatch = trimmed.match(/^url\s*=\s*(.+)$/i);
if (urlMatch) {
return urlMatch[1].trim();
}
}
}
return undefined;
}
/**
* Check if a tool name indicates it's an MCP (Model Context Protocol) tool.
* MCP tools are identified by names starting with "mcp." or "mcp_"
*/
export function isMcpTool(toolName: string): boolean {
return toolName.startsWith('mcp.') || toolName.startsWith('mcp_');
}
/**
* Normalize an MCP tool name so that equivalent tools from different servers
* (local stdio vs remote) are counted under a single canonical key in "By Tool" views.
* Maps mcp_github_github_<action> → mcp_io_github_git_<action>.
*/
export function normalizeMcpToolName(toolName: string): string {
if (toolName.startsWith('mcp_github_github_')) {
return 'mcp_io_github_git_' + toolName.substring('mcp_github_github_'.length);
}
if (toolName.startsWith('mcp.github.github.')) {
return 'mcp.io.github.git.' + toolName.substring('mcp.github.github.'.length);
}
return toolName;
}
/**
* Extract server name from an MCP tool name.
* MCP tool names follow the format: mcp.server.tool or mcp_server_tool
* For example: "mcp.io.github.git.assign_copilot_to_issue" → "GitHub MCP"
* Uses the display name from toolNames.json (the part before the colon).
* Falls back to extracting the second segment if no mapping exists.
*/
export function extractMcpServerName(toolName: string, toolNameMap: { [key: string]: string } = {}): string {
// First, try to get the display name from toolNames.json and extract the server part
const displayName = toolNameMap[toolName];
if (displayName && displayName.includes(':')) {
// Extract the part before the colon (e.g., "GitHub MCP" from "GitHub MCP: Issue Read")
return displayName.split(':')[0].trim();
}
// Fallback: recognize known MCP server prefixes for unlisted tools
if (toolName.startsWith('mcp_io_github_git_') || toolName.startsWith('mcp.io.github.git.')) {
return 'GitHub MCP (Local)';
}
if (toolName.startsWith('mcp_github_github_') || toolName.startsWith('mcp.github.github.')) {
return 'GitHub MCP (Remote)';
}
// Generic fallback: extract from tool name structure
const withoutPrefix = toolName.replace(/^mcp[._]/, '');
const parts = withoutPrefix.split(/[._]/);
return parts[0] || 'unknown';
}
/**
* Extract repository remote URL from file paths found in contentReferences.
* Looks for .git/config file in the workspace root to get the origin remote URL.
* @param contentReferences Array of content reference objects from session data
* @returns The repository remote URL if found, undefined otherwise
*/
export async function extractRepositoryFromContentReferences(contentReferences: any[]): Promise<string | undefined> {
if (!Array.isArray(contentReferences)) {
return undefined;
}
const filePaths: string[] = [];
// Collect all file paths from contentReferences
for (const contentRef of contentReferences) {
if (!contentRef || typeof contentRef !== 'object') {
continue;
}
let reference = null;
const kind = contentRef.kind;
if (kind === 'reference' && contentRef.reference) {
reference = contentRef.reference;
} else if (kind === 'inlineReference' && contentRef.inlineReference) {
reference = contentRef.inlineReference;
}
if (reference) {
// Prefer fsPath (native format) over path (URI format)
const rawPath = reference.fsPath || reference.path;
if (typeof rawPath === 'string' && rawPath.length > 0) {
// Convert VS Code URI path format to native path on Windows
// URI paths look like "/c:/Users/..." but should be "c:/Users/..." on Windows
let normalizedPath = rawPath;
if (process.platform === 'win32' && normalizedPath.match(/^\/[a-zA-Z]:/)) {
normalizedPath = normalizedPath.substring(1); // Remove leading slash
}
filePaths.push(normalizedPath);
}
}
}
if (filePaths.length === 0) {
return undefined;
}
// Find the most likely workspace root by looking for common parent directories
// Try each file path and look for a .git/config file in parent directories
const checkedRoots = new Set<string>();
for (const filePath of filePaths) {
// Normalize path separators to forward slashes for consistent splitting
const normalizedPath = filePath.replace(/\\/g, '/');
const pathParts = normalizedPath.split('/').filter(p => p.length > 0);
// Walk up the directory tree looking for .git/config
for (let i = pathParts.length - 1; i >= 1; i--) {
// Reconstruct path - on Windows, first part is drive letter (e.g., "c:")
let potentialRoot = pathParts.slice(0, i).join('/');
// On Windows, ensure we have a valid absolute path
if (process.platform === 'win32' && pathParts[0].match(/^[a-zA-Z]:$/)) {
// Path starts with drive letter, already valid
} else if (process.platform !== 'win32' && !potentialRoot.startsWith('/')) {
// On Unix, prepend / for absolute path
potentialRoot = '/' + potentialRoot;
}
// Skip if we've already checked this root
if (checkedRoots.has(potentialRoot)) {
continue;
}
checkedRoots.add(potentialRoot);
const gitConfigPath = path.join(potentialRoot, '.git', 'config');
try {
const gitConfig = await fs.promises.readFile(gitConfigPath, 'utf8');
const remoteUrl = parseGitRemoteUrl(gitConfig);
if (remoteUrl) {
return remoteUrl;
}
} catch {
// No .git/config at this level, continue up the tree
}
}
}
return undefined;
}
export function resolveWorkspaceFolderFromSessionPath(sessionFilePath: string, workspaceIdToFolderCache: Map<string, string | undefined>): string | undefined {
try {
// Normalize and split path into segments
const normalized = sessionFilePath.replace(/\\/g, '/');
const parts = normalized.split('/').filter(p => p.length > 0);
const idx = parts.findIndex(p => p.toLowerCase() === 'workspacestorage');
if (idx === -1 || idx + 1 >= parts.length) {
return undefined; // Not a workspace-scoped session file
}
const workspaceId = parts[idx + 1];
// Return cached value if present
if (workspaceIdToFolderCache.has(workspaceId)) {
return workspaceIdToFolderCache.get(workspaceId);
}
// Construct the workspaceStorage folder path by slicing the original normalized path
// This preserves absolute-root semantics on both Windows and Unix.
const workspaceSegment = `workspaceStorage/${workspaceId}`;
const lowerNormalized = normalized.toLowerCase();
const segmentIndex = lowerNormalized.indexOf(workspaceSegment.toLowerCase());
if (segmentIndex === -1) {
// Should not happen if parts detection succeeded, but guard just in case
workspaceIdToFolderCache.set(workspaceId, undefined);
return undefined;
}
const folderPathNormalized = normalized.substring(0, segmentIndex + workspaceSegment.length);
const workspaceStorageFolder = path.normalize(folderPathNormalized);
const workspaceJsonPath = path.join(workspaceStorageFolder, 'workspace.json');
const metaJsonPath = path.join(workspaceStorageFolder, 'meta.json');
let folderFsPath: string | undefined;
if (fs.existsSync(workspaceJsonPath)) {
folderFsPath = parseWorkspaceStorageJsonFile(workspaceJsonPath, ['folder', 'workspace', 'configuration', 'uri', 'path']);
} else if (fs.existsSync(metaJsonPath)) {
folderFsPath = parseWorkspaceStorageJsonFile(metaJsonPath, ['folder', 'uri', 'workspace', 'path']);
}
// Normalize to undefined if folderFsPath is falsy
if (!folderFsPath || folderFsPath.length === 0) {
workspaceIdToFolderCache.set(workspaceId, undefined);
return undefined;
}
// Canonicalize path casing using the real filesystem path.
// Different VS Code variants (Stable, Insiders, Cursor) may store the same folder with
// different drive-letter or path casing in their workspace.json (e.g. "C:\Users\" vs "c:\users\").
// realpathSync.native returns the true OS-level casing, so the same physical folder always
// produces the same Map key and is deduplicated correctly.
try {
folderFsPath = fs.realpathSync.native(folderFsPath);
} catch {
// Path may not exist on disk (deleted/moved repo); keep the parsed path as-is.
}
workspaceIdToFolderCache.set(workspaceId, folderFsPath);
return folderFsPath;
} catch (err) {
// On any error, cache undefined to avoid repeated failures
try {
const parts = sessionFilePath.replace(/\\/g, '/').split('/').filter(p => p.length > 0);
const idx = parts.findIndex(p => p.toLowerCase() === 'workspacestorage');
if (idx !== -1 && idx + 1 < parts.length) {
workspaceIdToFolderCache.set(parts[idx + 1], undefined);
}
} catch { }
return undefined;
}
}
export function getEditorTypeFromPath(filePath: string, isOpenCodeSessionFile?: (p: string) => boolean): string {
const normalizedPath = filePath.toLowerCase().replace(/\\/g, '/');
if (normalizedPath.includes('/.copilot/session-state/')) {
return 'Copilot CLI';
}
if (isOpenCodeSessionFile?.(filePath)) {
return 'OpenCode';
}
if (normalizedPath.includes('/code - insiders/') || normalizedPath.includes('/code%20-%20insiders/')) {
return 'VS Code Insiders';
}
if (normalizedPath.includes('/code - exploration/') || normalizedPath.includes('/code%20-%20exploration/')) {
return 'VS Code Exploration';
}
if (normalizedPath.includes('/vscodium/')) {
return 'VSCodium';
}
if (normalizedPath.includes('/cursor/')) {
return 'Cursor';
}
if (normalizedPath.includes('.vscode-server-insiders/')) {
return 'VS Code Server (Insiders)';
}
if (normalizedPath.includes('.vscode-server/') || normalizedPath.includes('.vscode-remote/')) {
return 'VS Code Server';
}
if (normalizedPath.includes('/code/')) {
return 'VS Code';
}
return 'Unknown';
}
/**
* Detect which editor the session file belongs to based on its path.
*/
export function detectEditorSource(filePath: string, isOpenCodeSessionFile?: (p: string) => boolean): string {
const lowerPath = filePath.toLowerCase().replace(/\\/g, '/');
if (lowerPath.includes('/.copilot/session-state/')) { return 'Copilot CLI'; }
if (isOpenCodeSessionFile?.(filePath)) { return 'OpenCode'; }
if (lowerPath.includes('cursor')) { return 'Cursor'; }
if (lowerPath.includes('code - insiders') || lowerPath.includes('code-insiders')) { return 'VS Code Insiders'; }
if (lowerPath.includes('vscodium')) { return 'VSCodium'; }
if (lowerPath.includes('windsurf')) { return 'Windsurf'; }
if (lowerPath.includes('code')) { return 'VS Code'; }
return 'Unknown';
}