Private/Sources.ps1
|
# Where each tool keeps what it knows about a working folder, and how a folder it recorded relates to the one in hand. # A source is one entry in the table at the foot of this file: a name and a finder. Adding a tool is adding an entry. function ConvertTo-PCContinuityClaudeName { # Claude names a project folder for the working folder's path, every character that is not a letter or a digit a hyphen. param([string]$Path) ($Path.TrimEnd('\', '/')) -replace '[^A-Za-z0-9]', '-' } function ConvertFrom-PCContinuityFileUri { param([string]$Uri) if ($Uri -notmatch '^file://') { return $null } $path = [uri]::UnescapeDataString(($Uri -replace '^file://', '')) if ($path -match '^/[A-Za-z]:') { $path = $path.Substring(1) } $path } function Get-PCContinuityClaudeFolder { # The working folder the sessions in a Claude project folder recorded for themselves. param([string]$Path) $files = @(Get-ChildItem -LiteralPath $Path -Filter *.jsonl -File -ErrorAction SilentlyContinue | Sort-Object LastWriteTimeUtc -Descending | Select-Object -First 3) foreach ($file in $files) { $reader = $null try { $reader = [IO.StreamReader]::new([IO.File]::Open($file.FullName, 'Open', 'Read', 'ReadWrite')) for ($i = 0; $i -lt 40 -and -not $reader.EndOfStream; $i++) { $line = $reader.ReadLine() if ($line -match '"cwd"\s*:\s*"((?:[^"\\]|\\.)*)"') { return [string](('"' + $Matches[1] + '"') | ConvertFrom-Json) } } } catch { Write-Verbose "Could not read $($file.FullName): $_" } finally { if ($reader) { $reader.Dispose() } } } } function Test-PCContinuitySimilar { param([string]$A, [string]$B) $x = ($A -replace '[^A-Za-z0-9]', '').ToLowerInvariant(); $y = ($B -replace '[^A-Za-z0-9]', '').ToLowerInvariant() $x.Length -ge 4 -and $y.Length -ge 4 -and $x -ne $y -and ($x.Contains($y) -or $y.Contains($x)) } function Get-PCContinuityMatch { # How a folder a tool recorded relates to the working folder: the same, one it lived at before, one of the same name, or one of a similar name. param($Context, [string]$RecordedPath) $key = ConvertTo-PCContinuityPathKey $RecordedPath $hit = $Context.Aliases | Where-Object { $_.Key -eq $key } | Select-Object -First 1 if ($hit) { if ($hit.Kind -eq 'known') { return @{ Level = 'known'; Why = "recorded for $RecordedPath, where this work lived before" } } return @{ Level = 'exact'; Why = 'recorded for this folder' } } $leaf = Get-PCContinuityLeaf $RecordedPath if ($leaf -and $leaf.ToLowerInvariant() -eq $Context.Leaf.ToLowerInvariant()) { return @{ Level = 'probable'; Why = "recorded for $RecordedPath, a folder of the same name" } } if ($Context.IncludeSimilar -and (Test-PCContinuitySimilar $leaf $Context.Leaf)) { return @{ Level = 'similar'; Why = "recorded for $RecordedPath, a folder with a similar name" } } } function New-PCContinuityCandidate { param($Context, [string]$Tool, [string]$Source, [string]$Target, [string]$Path, $Match) $files = @(Get-ChildItem -LiteralPath $Path -Recurse -File -Force -ErrorAction SilentlyContinue) $accepted = [bool](@($Context.State['accepted']) | Where-Object { $_ -and $_['machine'] -eq $Context.Machine -and $_['source'] -eq $Source }) [pscustomobject]@{ Tool = $Tool Source = $Source Level = $Match.Level Why = $Match.Why Taken = ($Match.Level -in 'exact', 'known') -or $accepted Accepted = $accepted Files = $files.Count Bytes = [long](($files | Measure-Object Length -Sum).Sum) LastWrite = ($files | Sort-Object LastWriteTimeUtc | Select-Object -Last 1).LastWriteTimeUtc Path = $Path Target = $Target } } function Get-PCContinuitySourceRoot { param([string]$UserHome, [string]$AppDataPath) if (-not $UserHome) { $UserHome = [Environment]::GetFolderPath('UserProfile') } if (-not $AppDataPath) { $AppDataPath = switch (Get-PCContinuityOs) { 'windows' { [Environment]::GetFolderPath('ApplicationData') } 'macos' { Join-Path $UserHome 'Library' 'Application Support' } default { Join-Path $UserHome '.config' } } } @{ claude = Join-Path $UserHome '.claude' 'projects' vscode = @('Code', 'Code - Insiders') | ForEach-Object { @{ Variant = ($_ -replace '[^A-Za-z0-9]+', '-'); Path = Join-Path $AppDataPath $_ 'User' 'workspaceStorage' } } } } function Get-PCContinuityContext { # Everything a finder needs: the working folder under every spelling known for it, this machine, the state kept in the context repository. param([string]$Workspace, [string]$ContextPath, [string[]]$Alias, [string]$UserHome, [string]$AppDataPath, [string]$MachineName, [switch]$IncludeSimilar) $machine = Get-PCContinuityMachine $MachineName $state = if ($ContextPath) { Read-PCContinuityJson (Join-Path $ContextPath 'continuity.json') } if (-not $state) { $state = @{ homes = @(); accepted = @() } } $aliases = [Collections.Generic.List[object]]::new(); $seen = @{} $add = { param($path, $kind) if (-not $path) { return } $key = ConvertTo-PCContinuityPathKey $path if ($seen.ContainsKey($key)) { return } $seen[$key] = $true $aliases.Add([pscustomobject]@{ Path = $path; Key = $key; Kind = $kind }) } & $add $Workspace 'workspace' & $add (Get-PCContinuityRealPath $Workspace) 'workspace' foreach ($a in @($Alias)) { & $add $a 'workspace' } $workspaceKeys = @($aliases | ForEach-Object Key) foreach ($h in @($state['homes'])) { if (-not $h -or $h['machine'] -ne $machine) { continue } $paths = @($h['path']) + @($h['aliases']) $isHere = [bool]($paths | Where-Object { $_ -and (ConvertTo-PCContinuityPathKey $_) -in $workspaceKeys }) foreach ($p in $paths) { & $add $p $(if ($isHere) { 'workspace' } else { 'known' }) } } @{ Workspace = $Workspace Leaf = Get-PCContinuityLeaf $Workspace Machine = $machine Os = Get-PCContinuityOs State = $state Aliases = $aliases Roots = Get-PCContinuitySourceRoot -UserHome $UserHome -AppDataPath $AppDataPath IncludeSimilar = [bool]$IncludeSimilar ContextPath = $ContextPath } } function Get-PCContinuityCandidate { param($Context) $rank = @{ exact = 0; known = 1; probable = 2; similar = 3 } $found = foreach ($source in $script:PCContinuitySources) { & $source.Find $Context } @($found | Where-Object { $_ } | Sort-Object { $rank[$_.Level] }, Tool, Source) } $script:PCContinuitySources = @( @{ Name = 'claude' What = 'Claude Code: sessions, their tool results and the memory, one folder per working folder under ~/.claude/projects. The command line and the editor extension share it.' Find = { param($ctx) $root = $ctx.Roots.claude if (-not (Test-Path -LiteralPath $root)) { return } $byName = @{} foreach ($a in $ctx.Aliases) { $n = (ConvertTo-PCContinuityClaudeName $a.Path).ToLowerInvariant() if (-not $byName.ContainsKey($n)) { $byName[$n] = $a } } $leaf = (ConvertTo-PCContinuityClaudeName $ctx.Leaf).ToLowerInvariant() foreach ($dir in Get-ChildItem -LiteralPath $root -Directory | Sort-Object Name) { $name = $dir.Name.ToLowerInvariant(); $match = $null if ($byName.ContainsKey($name)) { $a = $byName[$name] $match = if ($a.Kind -eq 'known') { @{ Level = 'known'; Why = "named for $($a.Path), where this work lived before" } } else { @{ Level = 'exact'; Why = 'named for this folder''s path' } } } else { $recorded = Get-PCContinuityClaudeFolder $dir.FullName if ($recorded) { $match = Get-PCContinuityMatch $ctx $recorded } elseif ($name.EndsWith("-$leaf")) { $match = @{ Level = 'probable'; Why = 'its name ends with this folder''s name' } } elseif ($ctx.IncludeSimilar -and $name.Contains($leaf)) { $match = @{ Level = 'similar'; Why = 'its name contains this folder''s name' } } } if ($match) { New-PCContinuityCandidate -Context $ctx -Tool 'claude' -Source "claude:$($dir.Name)" -Target "claude/$($dir.Name)" -Path $dir.FullName -Match $match } } } } @{ Name = 'copilot' What = 'GitHub Copilot in VS Code: chat sessions and extension state, one folder per working folder under the editor''s workspace storage.' Find = { param($ctx) foreach ($root in $ctx.Roots.vscode) { if (-not (Test-Path -LiteralPath $root.Path)) { continue } foreach ($dir in Get-ChildItem -LiteralPath $root.Path -Directory | Sort-Object Name) { $file = Join-Path $dir.FullName 'workspace.json' if (-not (Test-Path -LiteralPath $file)) { continue } $recorded = $null try { $w = Get-Content -LiteralPath $file -Raw | ConvertFrom-Json -AsHashtable if ($w['folder']) { $recorded = ConvertFrom-PCContinuityFileUri $w['folder'] } elseif ($w['workspace']) { $p = ConvertFrom-PCContinuityFileUri $w['workspace']; if ($p) { $recorded = ($p -replace '[\\/][^\\/]+$', '') } } } catch { Write-Verbose "Could not read $file`: $_" } if (-not $recorded) { continue } $match = Get-PCContinuityMatch $ctx $recorded if ($match) { New-PCContinuityCandidate -Context $ctx -Tool 'copilot' -Source "copilot:$($root.Variant)/$($dir.Name)" -Target "copilot/$($root.Variant)/$($dir.Name)" -Path $dir.FullName -Match $match } } } } } @{ Name = 'specstory' What = 'SpecStory: its history of the chats, kept in .specstory in the working folder itself.' Find = { param($ctx) $path = Join-Path $ctx.Workspace '.specstory' if (Test-Path -LiteralPath $path -PathType Container) { New-PCContinuityCandidate -Context $ctx -Tool 'specstory' -Source 'specstory:.specstory' -Target 'specstory' -Path $path -Match @{ Level = 'exact'; Why = 'in this folder' } } } } ) |