dsh-config-sync-plugin 0.0.0-stage → 0.3.56
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.en.md +656 -0
- package/README.md +626 -2
- package/client.js +3208 -0
- package/cordis.patch.yml +16 -0
- package/index.js +4281 -0
- package/package.json +58 -4
- package/runtime/apply.ps1 +1406 -0
- package/runtime/capture.ps1 +1188 -0
- package/runtime/dsh-sync-common.ps1 +1002 -0
- package/runtime/install-plugin.ps1 +179 -0
- package/runtime/post-exit.ps1 +809 -0
- package/runtime/restore.ps1 +537 -0
- package/runtime/sync.ps1 +1029 -0
- package/runtime/tools/fix-session-cwd.mjs +200 -0
- package/tools/clientcheck.mjs +259 -0
- package/tools/pack-plugin.ps1 +79 -0
- package/tools/selftest.mjs +1461 -0
|
@@ -0,0 +1,1002 @@
|
|
|
1
|
+
# DSH 配置同步 —— apply.ps1 / capture.ps1 共用的常量与函数
|
|
2
|
+
#
|
|
3
|
+
# 为什么单独抽一个文件:
|
|
4
|
+
# 两个脚本原来各自复制了一份 Say/Ok/Warn/Die、路径段计算、工作区忽略规则、安装目录正则。
|
|
5
|
+
# 改一端忘另一端就会出现「capture 归一了、apply 没展开」这类只有跨机才暴露的错,
|
|
6
|
+
# 所以统一收在这里,两个脚本用 dot-source 引入(. "$PSScriptRoot\dsh-sync-common.ps1")。
|
|
7
|
+
#
|
|
8
|
+
# 纪律:本文件被两个脚本共用,改完必须两端都跑一遍回归(capture → git status 应干净)。
|
|
9
|
+
|
|
10
|
+
$BS = [string][char]92
|
|
11
|
+
# git 的输出是 UTF-8,Windows PowerShell 5.1 默认按当前代码页(936)读管道,
|
|
12
|
+
# 中文 commit message 到日志里就成了「鎺掗槦」这种乱码。统一按 UTF-8 解。
|
|
13
|
+
try { [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false) } catch { }
|
|
14
|
+
try { $OutputEncoding = New-Object System.Text.UTF8Encoding($false) } catch { }
|
|
15
|
+
$AppRootToken = '{{APP_ROOT}}'
|
|
16
|
+
$WorkspaceHomeToken = '{{WORKSPACE_HOME}}'
|
|
17
|
+
# 只出现在会话头 cwd 归一里(<home>\im 这类不属于前两个 token 的路径),apply 端不需要展开
|
|
18
|
+
$DshHomeToken = '{{DSH_HOME}}'
|
|
19
|
+
|
|
20
|
+
function Say($m) { Write-Host $m }
|
|
21
|
+
function Ok($m) { Write-Host " [OK] $m" -ForegroundColor Green }
|
|
22
|
+
function Warn($m) { Write-Host " [!] $m" -ForegroundColor Yellow }
|
|
23
|
+
function Die($m) { Write-Host " [X] $m" -ForegroundColor Red; exit 1 }
|
|
24
|
+
|
|
25
|
+
# ---------- 未同步文件清单(notices) ----------
|
|
26
|
+
# 为什么单独攒一份文件:capture / sync / apply 是不同进程,各自只能写日志,用户看不到
|
|
27
|
+
# 「哪些文件这次没同步」;post-exit 在进度窗口末尾要汇总它,面板(index.js / client.js)
|
|
28
|
+
# 要常驻显示它。所以统一落到 ~\.dsh\sync-notices.json,谁跑谁追加,收尾谁读谁用。
|
|
29
|
+
# kind:
|
|
30
|
+
# too-large 超过单文件上限(capture 跳过 / push 前从索引剔除 / 历史里已存在)
|
|
31
|
+
# skipped 被规则或占用跳过(不是失败,但用户该知道)
|
|
32
|
+
# failed 写入失败、被远端拒绝等真失败
|
|
33
|
+
# history 历史提交里已存在的超限文件(远端必然拒收,必须改写历史或换仓库)
|
|
34
|
+
$script:SyncNoticeItems = New-Object 'System.Collections.ArrayList'
|
|
35
|
+
$script:SyncNoticeHome = $null
|
|
36
|
+
$script:SyncNoticeMode = ''
|
|
37
|
+
$script:SyncNoticeLoaded = $false
|
|
38
|
+
|
|
39
|
+
function Get-SyncNoticesPath([string]$dshHome) {
|
|
40
|
+
$h = if ($dshHome) { $dshHome } else { Join-Path $env:USERPROFILE '.dsh' }
|
|
41
|
+
return (Join-Path $h 'sync-notices.json')
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function Read-SyncNotices([string]$dshHome) {
|
|
45
|
+
try {
|
|
46
|
+
$p = Get-SyncNoticesPath $dshHome
|
|
47
|
+
if (-not (Test-Path -LiteralPath $p)) { return $null }
|
|
48
|
+
return (Get-Content -LiteralPath $p -Raw -Encoding utf8 | ConvertFrom-Json)
|
|
49
|
+
} catch { return $null }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function Save-SyncNotices {
|
|
53
|
+
try {
|
|
54
|
+
$p = Get-SyncNoticesPath $script:SyncNoticeHome
|
|
55
|
+
$dir = Split-Path -Parent $p
|
|
56
|
+
if ($dir -and -not (Test-Path -LiteralPath $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null }
|
|
57
|
+
$byKind = [ordered]@{}
|
|
58
|
+
foreach ($k in @('too-large', 'history', 'skipped', 'failed')) {
|
|
59
|
+
$byKind[$k] = @($script:SyncNoticeItems | Where-Object { $_.kind -eq $k }).Count
|
|
60
|
+
}
|
|
61
|
+
$obj = [ordered]@{
|
|
62
|
+
at = [long]([DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds())
|
|
63
|
+
mode = $script:SyncNoticeMode
|
|
64
|
+
total = $script:SyncNoticeItems.Count
|
|
65
|
+
byKind = $byKind
|
|
66
|
+
items = @($script:SyncNoticeItems)
|
|
67
|
+
}
|
|
68
|
+
[IO.File]::WriteAllText($p, ($obj | ConvertTo-Json -Depth 4), (New-Object Text.UTF8Encoding $false))
|
|
69
|
+
} catch { }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
# 每个动作开始时清空。被 sync 调起的子脚本(capture)要传 -NoResetNotices,
|
|
73
|
+
# 否则它一 Reset 就把父进程已经攒下的条目抹掉。
|
|
74
|
+
function Reset-SyncNotices([string]$dshHome, [string]$mode) {
|
|
75
|
+
$script:SyncNoticeHome = $dshHome
|
|
76
|
+
$script:SyncNoticeMode = $mode
|
|
77
|
+
$script:SyncNoticeItems.Clear()
|
|
78
|
+
$script:SyncNoticeLoaded = $true
|
|
79
|
+
Save-SyncNotices
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function Add-SyncNotice {
|
|
83
|
+
param([string]$Kind = 'skipped', [string]$Path = '', [string]$Reason = '', [long]$Bytes = 0)
|
|
84
|
+
if (-not $Path) { return }
|
|
85
|
+
# 先吃进磁盘上别的进程已经写好的条目(capture 与 sync 是两个进程)
|
|
86
|
+
if (-not $script:SyncNoticeLoaded) {
|
|
87
|
+
$existing = Read-SyncNotices $script:SyncNoticeHome
|
|
88
|
+
if ($existing -and $existing.items) {
|
|
89
|
+
foreach ($it in @($existing.items)) {
|
|
90
|
+
[void]$script:SyncNoticeItems.Add([ordered]@{ kind = [string]$it.kind; path = [string]$it.path; bytes = [long]$it.bytes; reason = [string]$it.reason })
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
$script:SyncNoticeLoaded = $true
|
|
94
|
+
}
|
|
95
|
+
foreach ($it in $script:SyncNoticeItems) {
|
|
96
|
+
if ($it.kind -eq $Kind -and $it.path -eq $Path) { return }
|
|
97
|
+
}
|
|
98
|
+
[void]$script:SyncNoticeItems.Add([ordered]@{ kind = $Kind; path = $Path; bytes = $Bytes; reason = $Reason })
|
|
99
|
+
Save-SyncNotices
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
# 给窗口/面板用的一行摘要:「2 个超过单文件上限、1 个被远端拒绝」
|
|
103
|
+
function Get-SyncNoticesSummary($notices) {
|
|
104
|
+
if (-not $notices -or -not $notices.items -or @($notices.items).Count -eq 0) { return '' }
|
|
105
|
+
$bk = $notices.byKind
|
|
106
|
+
$parts = @()
|
|
107
|
+
if ($bk.'too-large' -gt 0) { $parts += ("{0} 个超过单文件上限" -f $bk.'too-large') }
|
|
108
|
+
if ($bk.history -gt 0) { $parts += ("{0} 个历史提交里的超限文件" -f $bk.history) }
|
|
109
|
+
if ($bk.skipped -gt 0) { $parts += ("{0} 个被规则跳过" -f $bk.skipped) }
|
|
110
|
+
if ($bk.failed -gt 0) { $parts += ("{0} 个写入/推送失败" -f $bk.failed) }
|
|
111
|
+
return ($parts -join '、')
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
# Windows PowerShell 5.1 的模块自动加载偶尔拿不到 Get-FileHash(PSModulePath 被别的版本目录污染时,
|
|
115
|
+
# 会先撞上一个装不进 5.1 的同名模块,之后这个 cmdlet 就"不存在"了,脚本直接 CommandNotFound 崩掉)。
|
|
116
|
+
# 同步链路全靠它判断"文件要不要覆盖",所以缺了就补一个等价实现,不让一个 cmdlet 决定整条链路生死。
|
|
117
|
+
if (-not (Get-Command Get-FileHash -ErrorAction SilentlyContinue)) {
|
|
118
|
+
function Get-FileHash {
|
|
119
|
+
param(
|
|
120
|
+
[Parameter(Mandatory = $true)][string]$LiteralPath,
|
|
121
|
+
[ValidateSet('SHA256', 'SHA1', 'MD5')][string]$Algorithm = 'SHA256'
|
|
122
|
+
)
|
|
123
|
+
$algo = [System.Security.Cryptography.HashAlgorithm]::Create($Algorithm)
|
|
124
|
+
$fs = [System.IO.File]::Open($LiteralPath, 'Open', 'Read', 'ReadWrite')
|
|
125
|
+
try { $bytes = $algo.ComputeHash($fs) } finally { $fs.Dispose(); $algo.Dispose() }
|
|
126
|
+
[pscustomobject]@{
|
|
127
|
+
Algorithm = $Algorithm
|
|
128
|
+
Hash = (($bytes | ForEach-Object { $_.ToString('X2') }) -join '')
|
|
129
|
+
Path = $LiteralPath
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
# 默认工作区(<WorkspaceHome>\default-workspace)不参与同步。
|
|
135
|
+
# 原因:它的目录名由「本机 WorkspaceHome 绝对路径」编码而来,两台机器的 WorkspaceHome 天然不同,
|
|
136
|
+
# 同步过去只会在目标机器上留下一个"文件在、但 DSH 按自己的编码找不到"的目录(表现为该工作区历史为空)。
|
|
137
|
+
# 判据只看尾段:编码名一律以 "-default-workspace--" 收尾(含 CJK 转义段也不影响)。
|
|
138
|
+
function Test-DefaultWorkspaceDir([string]$dirName) {
|
|
139
|
+
if (-not $dirName) { return $false }
|
|
140
|
+
return ($dirName -like '*-default-workspace--')
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
# git 常常不在当前进程的 PATH 里(尤其是从客户端里起的命令行),按常见安装位置兜底。
|
|
144
|
+
# 以前 sync.ps1 / restore.ps1 / post-exit.ps1 各写一份,位置表还不一致(restore 那份写死 C:\Program Files
|
|
145
|
+
# 且不认 LOCALAPPDATA),合并到这里。返回 $null 表示没找到,由调用方决定报错还是降级。
|
|
146
|
+
# post-exit.ps1 例外:它要在仓库目录缺失时也能跑,所以自带一份最小实现(见该文件注释)。
|
|
147
|
+
function Get-GitExe {
|
|
148
|
+
foreach ($p in @(
|
|
149
|
+
(Join-Path $env:ProgramFiles 'Git\cmd\git.exe'),
|
|
150
|
+
(Join-Path $env:LOCALAPPDATA 'Programs\Git\cmd\git.exe'),
|
|
151
|
+
(Join-Path ${env:ProgramFiles(x86)} 'Git\cmd\git.exe')
|
|
152
|
+
)) {
|
|
153
|
+
if ($p -and (Test-Path -LiteralPath $p)) { return $p }
|
|
154
|
+
}
|
|
155
|
+
$c = Get-Command git.exe -ErrorAction SilentlyContinue
|
|
156
|
+
if ($c) { return $c.Source }
|
|
157
|
+
return $null
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
# 求 $full 相对 $base 的路径:按路径段数取尾巴,避免 8.3 短路径(ADMINI~1)或大小写差异导致 Substring 算错
|
|
161
|
+
function Get-RelPath([string]$base, [string]$full) { $b = @($base -split '\\' | Where-Object { $_ -ne '' })
|
|
162
|
+
$f = @($full -split '\\' | Where-Object { $_ -ne '' })
|
|
163
|
+
if ($f.Count -le $b.Count) { return $null }
|
|
164
|
+
return ($f[$b.Count..($f.Count - 1)] -join '\')
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
# 把 $srcDir 的内容合并进 $dstDir:目标已存在同名子目录时不能再用 Copy-Item -Recurse(会套娃成 x\x)
|
|
168
|
+
function CopyMerge($srcDir, $dstDir) {
|
|
169
|
+
if (-not (Test-Path -LiteralPath $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
|
|
170
|
+
Get-ChildItem -LiteralPath $srcDir -Force | ForEach-Object {
|
|
171
|
+
$d = Join-Path $dstDir $_.Name
|
|
172
|
+
if ($_.PSIsContainer) { CopyMerge $_.FullName $d }
|
|
173
|
+
else { Copy-Item -LiteralPath $_.FullName -Destination $d -Force }
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
# ---------- 工作区资料:忽略清单 ----------
|
|
178
|
+
$wsSkipDirs = @('.git','node_modules','sessions','attachments','logs','_tmp','staging','.dsh','.agent-teams','models')
|
|
179
|
+
$wsSkipFiles = @('Thumbs.db','desktop.ini','.DS_Store')
|
|
180
|
+
$wsSkipExt = @('.bak','.tmp','.log','.whl','.safetensors','.ckpt')
|
|
181
|
+
# 单文件上限:Gitee 免费版单文件 100MB、仓库总量 1GB。超过一律不进仓库——
|
|
182
|
+
# 真实事故:默认工作区的 img-gen\models 里 sd_xl_base_1.0.safetensors(6.6GB) + 三个 torch*.whl(各 2.3GB)
|
|
183
|
+
# 共 13.8GB 被同步进仓库,既拖垮每次同步、又把云端配额顶爆(push 被 pre-receive 拒绝)。
|
|
184
|
+
# 这类大文件本来就该留在本机(原件在工作区目录里不动),同步只搬配置/资料。
|
|
185
|
+
$wsMaxFileBytes = 100MB
|
|
186
|
+
|
|
187
|
+
function Test-WorkspaceIgnored([string]$rel) {
|
|
188
|
+
# 只按「目录段」判忽略,最后一段是文件名不算目录(否则一个恰好叫 logs 的文件会被误跳过)
|
|
189
|
+
$parts = @($rel.Replace($BS,'/') -split '/' | Where-Object { $_ -ne '' })
|
|
190
|
+
$dirParts = if ($parts.Count -gt 1) { @($parts[0..($parts.Count-2)]) } else { @() }
|
|
191
|
+
foreach ($seg in $dirParts) { if ($wsSkipDirs -contains $seg) { return $true } }
|
|
192
|
+
$leaf = if ($parts.Count -gt 0) { $parts[$parts.Count-1] } else { '' }
|
|
193
|
+
if ($wsSkipFiles -contains $leaf) { return $true }
|
|
194
|
+
if ($wsSkipExt -contains ([IO.Path]::GetExtension($leaf).ToLower())) { return $true }
|
|
195
|
+
if ($leaf -like '*.bak-*') { return $true }
|
|
196
|
+
return $false
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
# ---------- 客户端安装目录 ----------
|
|
200
|
+
# 仓库里凡是「客户端安装目录」的绝对路径,一律存成 {{APP_ROOT}}/ 前缀;
|
|
201
|
+
# apply 时展开成本机路径,capture 时归一成占位符。
|
|
202
|
+
#
|
|
203
|
+
# 关键修复(2026-11-10):旧版正则把目录名写死成 DeepSeekHarness,
|
|
204
|
+
# 客户端装在 D:\DSH 时占位符机制整体失效(本机路径原样入库 → 另一台 lock 与 package.json 对不上 → install 半装 → 启动崩)。
|
|
205
|
+
# 现在按「本机 -AppRoot 的实际末级目录名」生成正则:DSH、DeepSeekHarness、任意目录名都能命中。
|
|
206
|
+
function Get-AppRootLeaf([string]$appRoot) {
|
|
207
|
+
$leaf = Split-Path -Leaf ($appRoot.TrimEnd('\', '/'))
|
|
208
|
+
if ([string]::IsNullOrWhiteSpace($leaf)) { $leaf = 'DeepSeekHarness' }
|
|
209
|
+
return $leaf
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function Get-AppRootPattern([string]$appRoot) {
|
|
213
|
+
$leaf = Get-AppRootLeaf $appRoot
|
|
214
|
+
# 匹配 [盘符]:[\...\]<末级目录名>,含 JSON 双反斜杠写法;不匹配 <名字>-xxx 这类更长的目录
|
|
215
|
+
return ('(?i)(?<![A-Za-z0-9_])[A-Z]:[\\/]+(?:[^\s"''`,;()\[\]]*[\\/]+)?' + [regex]::Escape($leaf) + '(?![\w-])(?:[\\/]+)?')
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function Get-AppRootFwd([string]$appRoot) {
|
|
219
|
+
return ($appRoot -replace '\\', '/').TrimEnd('/')
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
# 把文本里「本机安装目录」的各种写法整体替换成 $replacement。
|
|
223
|
+
# 先做字面替换(覆盖任意目录名、任意盘符,这是主路径),再用正则兜底别的机器残留的同名目录。
|
|
224
|
+
function Convert-AppRootText([string]$text, [string]$appRoot, [string]$replacement) {
|
|
225
|
+
$fwd = Get-AppRootFwd $appRoot
|
|
226
|
+
$plain = $appRoot.TrimEnd('\', '/')
|
|
227
|
+
$text = $text.Replace($plain, $replacement)
|
|
228
|
+
$text = $text.Replace($fwd, $replacement)
|
|
229
|
+
# JSON 里的双反斜杠写法:D:\\DeepSeekHarness
|
|
230
|
+
$text = $text.Replace(($plain -replace '\\', '\\\\'), ($replacement -replace '/', '\\\\'))
|
|
231
|
+
# 兜底:别的机器残留的同名目录(任意盘符)
|
|
232
|
+
$pattern = Get-AppRootPattern $appRoot
|
|
233
|
+
return [regex]::Replace($text, $pattern, ($replacement + '/'))
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
# ---------- 文本读写(保 BOM) ----------
|
|
237
|
+
function Read-TextPreserveBom([string]$path) {
|
|
238
|
+
$bytes = [IO.File]::ReadAllBytes($path)
|
|
239
|
+
$text = [Text.Encoding]::UTF8.GetString($bytes)
|
|
240
|
+
if ($text.Length -gt 0 -and $text[0] -eq [char]0xFEFF) { $text = $text.Substring(1) }
|
|
241
|
+
return $text
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function Write-TextPreserveBom([string]$path, [string]$text) {
|
|
245
|
+
$hasBom = $false
|
|
246
|
+
if (Test-Path -LiteralPath $path) {
|
|
247
|
+
$b = [IO.File]::ReadAllBytes($path)
|
|
248
|
+
$hasBom = ($b.Length -ge 3 -and $b[0] -eq 0xEF -and $b[1] -eq 0xBB -and $b[2] -eq 0xBF)
|
|
249
|
+
}
|
|
250
|
+
[IO.File]::WriteAllText($path, $text, (New-Object Text.UTF8Encoding($hasBom, $false)))
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
# ---------- 宿主版本 ----------
|
|
254
|
+
# 真正的 app 版本在 <AppRoot>\resources\app\package.json 的 version;
|
|
255
|
+
# <AppRoot>\version 里是 Electron 版本(44.7.0 这种),拿它当版本依据会误判「两端已对齐」。
|
|
256
|
+
#
|
|
257
|
+
# 两种客户端构建都要认:
|
|
258
|
+
# 解包版 → <AppRoot>\resources\app\package.json
|
|
259
|
+
# 打包版 → <AppRoot>\resources\app.asar(里面仍有 dsh\package.json,版本与本机 app 版一致)
|
|
260
|
+
# 只认前者时,打包版机器会「读不到版本」→ 跳过校验,bundle 探路也全落空。
|
|
261
|
+
function Get-AsarEntryText([string]$asar, [string]$entryPath) {
|
|
262
|
+
# entryPath 用 '/' 分段,例如 'dsh/package.json';读不到返回 $null。
|
|
263
|
+
# asar 结构:16 字节头(偏移 4 = header 长度,偏移 12 = JSON 长度,数据从 8+headerSize 起算)。
|
|
264
|
+
#
|
|
265
|
+
# 这里**不整体 JSON.parse**:asar 头部有 5.7MB,而 PowerShell 5.1 的 ConvertFrom-Json
|
|
266
|
+
# (JavaScriptSerializer)默认上限 2MB,直接解析必然抛 "length of the string exceeds"。
|
|
267
|
+
# 改为在头部文本里定位锚点 '"dsh":{"files":{' 后的第一个 '"package.json":{',
|
|
268
|
+
# 从它紧邻的 "size"/"offset" 取值,再把那一段小文件读出来(小文件解析没有上限问题)。
|
|
269
|
+
if (-not (Test-Path -LiteralPath $asar)) { return $null }
|
|
270
|
+
if ($entryPath -notmatch '^(.+)/([^/]+)$') { return $null }
|
|
271
|
+
$parent = $Matches[1]
|
|
272
|
+
$leaf = $Matches[2]
|
|
273
|
+
$fs = $null
|
|
274
|
+
try {
|
|
275
|
+
$fs = [IO.File]::Open($asar, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::ReadWrite)
|
|
276
|
+
$head = New-Object byte[] 16
|
|
277
|
+
if ($fs.Read($head, 0, 16) -ne 16) { return $null }
|
|
278
|
+
$headerSize = [int64][BitConverter]::ToUInt32($head, 4)
|
|
279
|
+
$jsonLen = [int][BitConverter]::ToUInt32($head, 12)
|
|
280
|
+
if ($jsonLen -lt 2 -or $jsonLen -gt 200MB) { return $null }
|
|
281
|
+
$jsonBytes = New-Object byte[] $jsonLen
|
|
282
|
+
$read = 0
|
|
283
|
+
while ($read -lt $jsonLen) {
|
|
284
|
+
$n = $fs.Read($jsonBytes, $read, $jsonLen - $read)
|
|
285
|
+
if ($n -le 0) { return $null }
|
|
286
|
+
$read += $n
|
|
287
|
+
}
|
|
288
|
+
$text = [Text.Encoding]::UTF8.GetString($jsonBytes)
|
|
289
|
+
$anchor = '"' + $parent + '":{"files":{'
|
|
290
|
+
$ai = $text.IndexOf($anchor)
|
|
291
|
+
if ($ai -lt 0) { return $null }
|
|
292
|
+
$ei = $text.IndexOf('"' + $leaf + '":{', $ai)
|
|
293
|
+
if ($ei -lt 0) { return $null }
|
|
294
|
+
$chunk = $text.Substring($ei, [Math]::Min(400, $text.Length - $ei))
|
|
295
|
+
$m = [regex]::Match($chunk, '"size":(\d+),"offset":"?(\d+)"?')
|
|
296
|
+
if (-not $m.Success) { return $null }
|
|
297
|
+
$size = [int64]$m.Groups[1].Value
|
|
298
|
+
$off = [int64]$m.Groups[2].Value
|
|
299
|
+
if ($size -le 0 -or $size -gt 8MB) { return $null }
|
|
300
|
+
$fs.Position = [int64](8 + $headerSize) + $off
|
|
301
|
+
$bytes = New-Object byte[] $size
|
|
302
|
+
$read = 0
|
|
303
|
+
while ($read -lt $size) {
|
|
304
|
+
$n = $fs.Read($bytes, $read, $size - $read)
|
|
305
|
+
if ($n -le 0) { return $null }
|
|
306
|
+
$read += $n
|
|
307
|
+
}
|
|
308
|
+
return [Text.Encoding]::UTF8.GetString($bytes)
|
|
309
|
+
} catch { return $null } finally { if ($fs) { $fs.Dispose() } }
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function Get-HostAppVersion([string]$appRoot) {
|
|
313
|
+
$p = Join-Path $appRoot 'resources\app\package.json'
|
|
314
|
+
if (Test-Path -LiteralPath $p) {
|
|
315
|
+
try { return [string](Get-Content -LiteralPath $p -Raw -Encoding utf8 | ConvertFrom-Json).version } catch { }
|
|
316
|
+
}
|
|
317
|
+
$asar = Join-Path $appRoot 'resources\app.asar'
|
|
318
|
+
if (Test-Path -LiteralPath $asar) {
|
|
319
|
+
$text = Get-AsarEntryText $asar 'dsh/package.json'
|
|
320
|
+
if ($text) {
|
|
321
|
+
try { return [string]($text | ConvertFrom-Json).version } catch { }
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return $null
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
# 宿主自带 bundle 的落点:解包版在 resources\app\dsh\node_modules,打包版在 app.asar.unpacked\dsh\node_modules。
|
|
328
|
+
function Get-HostDshNodeModules([string]$appRoot) {
|
|
329
|
+
return @(
|
|
330
|
+
(Join-Path $appRoot 'resources\app\dsh\node_modules'),
|
|
331
|
+
(Join-Path $appRoot 'resources\app.asar.unpacked\dsh\node_modules')
|
|
332
|
+
)
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function Get-HostNodeExe([string]$appRoot) {
|
|
336
|
+
$p = Join-Path $appRoot 'resources\runtime\primary-runtime\dependencies\node\bin\node.exe'
|
|
337
|
+
if (Test-Path -LiteralPath $p) { return $p }
|
|
338
|
+
# 后台自动同步是从客户端进程拉起来的,PATH 里往往**没有** node(用户交互终端里才有)。
|
|
339
|
+
# 只认 PATH 会让「会话头部 cwd 修正」在无人值守时静默跳过,拉取后会话全部变成
|
|
340
|
+
# 带 {{APP_ROOT}} 占位符的文件,DSH 一个都不登记 —— 用户看到的就是「工作区还在、会话没了」。
|
|
341
|
+
# 所以这里把常见安装位置也探一遍。
|
|
342
|
+
$guesses = @()
|
|
343
|
+
foreach ($base in @($env:ProgramFiles, ${env:ProgramFiles(x86)}, $env:LOCALAPPDATA)) {
|
|
344
|
+
if (-not $base) { continue }
|
|
345
|
+
$guesses += (Join-Path $base 'nodejs\node.exe')
|
|
346
|
+
$guesses += (Join-Path $base 'Programs\nodejs\node.exe')
|
|
347
|
+
$guesses += (Join-Path $base 'Volta\bin\node.exe')
|
|
348
|
+
$guesses += (Join-Path $base 'fnm\node.exe')
|
|
349
|
+
}
|
|
350
|
+
foreach ($g in $guesses) {
|
|
351
|
+
if (Test-Path -LiteralPath $g) { return $g }
|
|
352
|
+
}
|
|
353
|
+
try {
|
|
354
|
+
$c = Get-Command node -ErrorAction Stop
|
|
355
|
+
if ($c -and $c.Source) { return $c.Source }
|
|
356
|
+
} catch { }
|
|
357
|
+
return $null
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
# 能跑 Node 脚本的「执行器」:优先真 Node;没有的话用客户端自带的 Electron
|
|
361
|
+
# (ELECTRON_RUN_AS_NODE=1 时它就是 node,自带 zstd,版本通常比用户装的还新)。
|
|
362
|
+
# 用户机器上经常根本没装独立 Node —— 只认 node.exe 会让会话头部永远修不了。
|
|
363
|
+
function Get-HostNodeRunner([string]$appRoot) {
|
|
364
|
+
$node = Get-HostNodeExe $appRoot
|
|
365
|
+
if ($node) { return @{ Exe = $node; Electron = $false } }
|
|
366
|
+
if (-not (Test-Path -LiteralPath $appRoot)) { return $null }
|
|
367
|
+
$exes = @(Get-ChildItem -LiteralPath $appRoot -Filter '*.exe' -File -ErrorAction SilentlyContinue |
|
|
368
|
+
Where-Object { $_.Name -notmatch 'Uninstall' })
|
|
369
|
+
$hostExe = $exes | Where-Object { $_.Name -match 'DeepSeek Harness' } | Select-Object -First 1
|
|
370
|
+
if (-not $hostExe) { $hostExe = $exes | Select-Object -First 1 }
|
|
371
|
+
if ($hostExe) { return @{ Exe = $hostExe.FullName; Electron = $true } }
|
|
372
|
+
return $null
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
$script:AsarBundleNameCache = @{}
|
|
376
|
+
# 打包版客户端把宿主 bundle 整个塞在 app.asar 里(dsh/node_modules/<pkg>),磁盘上**没有**对应目录。
|
|
377
|
+
# 只按路径判断会让 asar 版机器把 20 多个自带 bundle 全误报成「找不到」,进而误删实验性 bundle。
|
|
378
|
+
# 这里交给客户端自带 node 读 asar 头,列出包内 node_modules 下的包名(两级,够 @scope/name)。
|
|
379
|
+
function Get-AsarBundleNames([string]$appRoot) {
|
|
380
|
+
$asar = Join-Path $appRoot 'resources\app.asar'
|
|
381
|
+
if (-not (Test-Path -LiteralPath $asar)) { return $null }
|
|
382
|
+
$node = Get-HostNodeExe $appRoot
|
|
383
|
+
if (-not $node) { return $null }
|
|
384
|
+
if ($script:AsarBundleNameCache.ContainsKey($asar)) { return $script:AsarBundleNameCache[$asar] }
|
|
385
|
+
$js = @'
|
|
386
|
+
const fs=require('fs');
|
|
387
|
+
const a=process.argv[2];
|
|
388
|
+
const fd=fs.openSync(a,'r');const h=Buffer.alloc(16);fs.readSync(fd,h,0,16,0);
|
|
389
|
+
const jl=h.readUInt32LE(12);const b=Buffer.alloc(jl);fs.readSync(fd,b,0,jl,16);fs.closeSync(fd);
|
|
390
|
+
const t=JSON.parse(b.toString('utf8'));
|
|
391
|
+
const nm=(t.files&&t.files.dsh&&t.files.dsh.files&&t.files.dsh.files.node_modules)||(t.files&&t.files.node_modules);
|
|
392
|
+
const out=[];
|
|
393
|
+
(function walk(node,prefix,depth){
|
|
394
|
+
if(!node||!node.files||depth>2) return;
|
|
395
|
+
for(const [k,v] of Object.entries(node.files)){
|
|
396
|
+
const name=prefix?prefix+'/'+k:k;
|
|
397
|
+
if(v&&v.files){ out.push(name); walk(v,name,depth+1); }
|
|
398
|
+
}
|
|
399
|
+
})(nm,'',1);
|
|
400
|
+
process.stdout.write(JSON.stringify(out));
|
|
401
|
+
'@
|
|
402
|
+
$tmp = Join-Path $env:TEMP ('asar-names-' + [Guid]::NewGuid().ToString('N') + '.cjs')
|
|
403
|
+
try {
|
|
404
|
+
[IO.File]::WriteAllText($tmp, $js, (New-Object Text.UTF8Encoding($false)))
|
|
405
|
+
$raw = (& $node $tmp $asar 2>$null | Out-String)
|
|
406
|
+
$names = @($raw.Trim() | ConvertFrom-Json)
|
|
407
|
+
$script:AsarBundleNameCache[$asar] = $names
|
|
408
|
+
return $names
|
|
409
|
+
} catch {
|
|
410
|
+
$script:AsarBundleNameCache[$asar] = $null
|
|
411
|
+
return $null
|
|
412
|
+
} finally {
|
|
413
|
+
if (Test-Path -LiteralPath $tmp) { Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue }
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
# 判断 bundle 是否存在于宿主(路径 or asar 包内)。判不了返回 $true(宁可放过,也别误删)。
|
|
418
|
+
function Test-HostBundle([string]$appRoot, [string]$bundle) {
|
|
419
|
+
$rel = ($bundle -replace '/', '\')
|
|
420
|
+
foreach ($m in (Get-HostDshNodeModules $appRoot)) {
|
|
421
|
+
if (Test-Path -LiteralPath (Join-Path $m ($rel + '\package.json'))) { return $true }
|
|
422
|
+
}
|
|
423
|
+
$names = Get-AsarBundleNames $appRoot
|
|
424
|
+
if ($null -eq $names) { return $true }
|
|
425
|
+
return ($names -contains $bundle)
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function Get-HostElectronVersion([string]$appRoot) {
|
|
429
|
+
$p = Join-Path $appRoot 'version'
|
|
430
|
+
if (-not (Test-Path -LiteralPath $p)) { return $null }
|
|
431
|
+
return (Read-TextPreserveBom $p).Trim()
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
# ---------- 客户端根(AppRoot)自动定位 ----------
|
|
435
|
+
# 客户端可能装在任意盘/任意目录,也会被卸载重装到别处;脚本里写死一个盘符换机或换盘就废。
|
|
436
|
+
# 定位顺序:显式参数(且要对得上)→ 环境变量 → 注册表卸载项的 InstallLocation(最权威)→ 常见位置。
|
|
437
|
+
function Test-AppRootDir([string]$path) {
|
|
438
|
+
if ([string]::IsNullOrWhiteSpace($path)) { return $false }
|
|
439
|
+
$p = $path.TrimEnd('\', '/')
|
|
440
|
+
return (Test-Path -LiteralPath (Join-Path $p 'resources\app.asar')) -or (Test-Path -LiteralPath (Join-Path $p 'resources\app\package.json'))
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function Get-InstalledAppLocation([string]$namePattern) {
|
|
444
|
+
$roots = @(
|
|
445
|
+
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*',
|
|
446
|
+
'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*',
|
|
447
|
+
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*'
|
|
448
|
+
)
|
|
449
|
+
foreach ($r in $roots) {
|
|
450
|
+
try {
|
|
451
|
+
$items = Get-ItemProperty -Path $r -ErrorAction Stop
|
|
452
|
+
} catch { continue }
|
|
453
|
+
foreach ($it in $items) {
|
|
454
|
+
if (-not $it) { continue }
|
|
455
|
+
if ($it.DisplayName -and ($it.DisplayName -like $namePattern) -and $it.InstallLocation) {
|
|
456
|
+
if (Test-AppRootDir $it.InstallLocation) { return $it.InstallLocation.TrimEnd('\', '/') }
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return $null
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function Resolve-DshAppRoot([string]$explicit) {
|
|
464
|
+
$tried = @()
|
|
465
|
+
if ($explicit) {
|
|
466
|
+
if (Test-AppRootDir $explicit) { return $explicit.TrimEnd('\', '/') }
|
|
467
|
+
$tried += $explicit
|
|
468
|
+
}
|
|
469
|
+
if ($env:DSH_APP_ROOT) {
|
|
470
|
+
if (Test-AppRootDir $env:DSH_APP_ROOT) { return $env:DSH_APP_ROOT.TrimEnd('\', '/') }
|
|
471
|
+
$tried += $env:DSH_APP_ROOT
|
|
472
|
+
}
|
|
473
|
+
$reg = Get-InstalledAppLocation '*DeepSeek Harness*'
|
|
474
|
+
if ($reg) { return $reg }
|
|
475
|
+
$candidates = @()
|
|
476
|
+
foreach ($r in @($env:LOCALAPPDATA, $env:ProgramFiles, ${env:ProgramFiles(x86)})) {
|
|
477
|
+
if ($r) { $candidates += (Join-Path $r 'Programs\DeepSeekHarness'); $candidates += (Join-Path $r 'DeepSeekHarness') }
|
|
478
|
+
}
|
|
479
|
+
$candidates += 'D:\DeepSeekHarness', 'D:\DSH', 'G:\DeepSeekHarness', 'G:\DSH', 'E:\DeepSeekHarness'
|
|
480
|
+
if ($env:DSH_HOME) {
|
|
481
|
+
# .dsh 与客户端常在同一层盘位的不同目录,这里只做兜底试探
|
|
482
|
+
$candidates += (Join-Path (Split-Path -Parent $env:DSH_HOME) 'DeepSeekHarness')
|
|
483
|
+
}
|
|
484
|
+
foreach ($c in $candidates) {
|
|
485
|
+
if ($c -and (Test-AppRootDir $c)) { return $c.TrimEnd('\', '/') }
|
|
486
|
+
}
|
|
487
|
+
return $null
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
# ---------- 同步内容勾选(config-sync-scope.json)----------
|
|
491
|
+
# 面板「同步内容」卡片的落点:每组一行、组内还能排除单项。文件不存在 = 全勾(老行为不变)。
|
|
492
|
+
# 纪律:勾选只决定「要不要处理」——跳过就是既不抓也不还原;
|
|
493
|
+
# 只有 deleteCloud 打开时才顺手删掉仓库里那份副本(默认关闭,删了就不可逆)。
|
|
494
|
+
$SyncScopeGroups = @('profile', 'prompt', 'skills', 'presets', 'storages-record', 'settings', 'mnemon', 'local-plugins', 'plugin-data', 'extra', 'workspace', 'conversations')
|
|
495
|
+
|
|
496
|
+
# ---------- 插件数据 / 额外路径(0.3.51 起)----------
|
|
497
|
+
# 背景:以前的同步范围是白名单(十组固定清单),新装的插件如果自己往 <DshHome>\<插件名>\ 或
|
|
498
|
+
# 自定义目录里写配置/数据,默认一个都带不走。这里补两组:
|
|
499
|
+
# plugin-data —— 自动枚举 <DshHome> 的一级子目录(排除已被其他组覆盖的系统目录),每个插件一项,
|
|
500
|
+
# 递归收符合规则的小文件;
|
|
501
|
+
# extra —— 用户在面板上自己填的额外路径(文件或目录),一项一条。
|
|
502
|
+
# 两组的规则都写在配置文件 <DshHome>\config-sync-extra.json(本机,不进仓库):
|
|
503
|
+
# 排除名单可改、单文件上限可改(硬顶 100MB —— GitHub 单文件超过 100MB 会被拒收,调大也没用)。
|
|
504
|
+
$SyncExtraHardMaxBytes = 104857600
|
|
505
|
+
|
|
506
|
+
$SyncExtraDefaultSkipDirs = @(
|
|
507
|
+
'profiles', 'skills', '.agent-presets', 'storages', 'sessions', 'attachments', 'logs', 'backups',
|
|
508
|
+
'update-staging', 'electron', 'dsh-runtimes', 'dsh-market', '.dsh-plugin-market', 'dsh-session-manager',
|
|
509
|
+
'im', 'node_modules'
|
|
510
|
+
)
|
|
511
|
+
|
|
512
|
+
$SyncExtraDefaultExclude = @(
|
|
513
|
+
'*.db', '*.db-*', '*.db3', '*.sqlite', '*.sqlite3', '*.sqlite-*', '*.db-shm', '*.db-wal',
|
|
514
|
+
'*.node', '*.exe', '*.dll', '*.asar', '*.pak', '*.bin', '*.iso', '*.img',
|
|
515
|
+
'*.zip', '*.7z', '*.rar', '*.tar', '*.gz', '*.tgz', '*.mp4', '*.mkv', '*.avi',
|
|
516
|
+
'*.log', '*.log.*', '*.tmp', '*.bak', '*.bak-*', '*.old', '*.orig', '*.swp',
|
|
517
|
+
'api-key', '*.pem', '*.key', '*.p12', '*.pfx', '*.crt',
|
|
518
|
+
'*cache*', '*token*', '*secret*', '*credential*', '*password*', '*vault*',
|
|
519
|
+
'node_modules', '.git', '.cache', '__pycache__', '.venv'
|
|
520
|
+
)
|
|
521
|
+
|
|
522
|
+
# 凭据内容嗅探:名字看不出、内容里却带登录态的文件(workbuddy-vault\cn\<hash>.json 就是这种,
|
|
523
|
+
# 里面是 accessToken)不能进仓库。抓取时读前 8KB 判一次,命中就跳过并 Warn。
|
|
524
|
+
$SyncExtraSecretMarkers = @(
|
|
525
|
+
'"accesstoken"', '"refreshtoken"', '"idtoken"', '"apikey"', '"api_key"',
|
|
526
|
+
'"clientsecret"', '"privatekey"', '"password"', 'authorization: bearer'
|
|
527
|
+
)
|
|
528
|
+
|
|
529
|
+
function Test-SyncExtraSecret([string]$file) {
|
|
530
|
+
try {
|
|
531
|
+
if ((Get-Item -LiteralPath $file -Force).Length -gt 8MB) { return $false }
|
|
532
|
+
$bytes = [IO.File]::ReadAllBytes($file)
|
|
533
|
+
if ($bytes.Length -gt 8192) { $bytes = $bytes[0..8191] }
|
|
534
|
+
# 二进制里到处是 NUL,别拿它当文本嗅探(真正的二进制文件另有扩展名清单挡着)
|
|
535
|
+
if ($bytes -contains 0) { return $false }
|
|
536
|
+
$text = ([Text.Encoding]::UTF8.GetString($bytes)).ToLowerInvariant()
|
|
537
|
+
foreach ($m in $SyncExtraSecretMarkers) { if ($text.Contains($m)) { return $true } }
|
|
538
|
+
return $false
|
|
539
|
+
} catch { return $false }
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
# 自动枚举时被凭据嗅探挡下的文件:抓完集中 Warn 一次,用户知道"这些没进来、为什么"
|
|
543
|
+
$script:SyncExtraSecretSkips = New-Object System.Collections.ArrayList
|
|
544
|
+
function Reset-SyncExtraSecretSkips { $script:SyncExtraSecretSkips.Clear() }
|
|
545
|
+
function Get-SyncExtraSecretSkips { return @($script:SyncExtraSecretSkips) }
|
|
546
|
+
|
|
547
|
+
function Get-SyncExtraPath([string]$DshHome) { return (Join-Path $DshHome 'config-sync-extra.json') }
|
|
548
|
+
|
|
549
|
+
function Get-SyncExtraConfig([string]$DshHome) {
|
|
550
|
+
$pd = [pscustomobject]@{
|
|
551
|
+
enabled = $true
|
|
552
|
+
maxFileBytes = $SyncExtraHardMaxBytes
|
|
553
|
+
exclude = @($SyncExtraDefaultExclude)
|
|
554
|
+
skipDirs = @($SyncExtraDefaultSkipDirs)
|
|
555
|
+
}
|
|
556
|
+
$ex = [pscustomobject]@{ enabled = $true; paths = @() }
|
|
557
|
+
$f = $null
|
|
558
|
+
if ($DshHome) { $f = Get-SyncExtraPath $DshHome }
|
|
559
|
+
if ($f -and (Test-Path -LiteralPath $f)) {
|
|
560
|
+
try {
|
|
561
|
+
$j = Get-Content -LiteralPath $f -Raw -Encoding utf8 | ConvertFrom-Json
|
|
562
|
+
if ($j.pluginData) {
|
|
563
|
+
if ($null -ne $j.pluginData.PSObject.Properties['enabled']) { $pd.enabled = ($j.pluginData.enabled -ne $false) }
|
|
564
|
+
if ($null -ne $j.pluginData.PSObject.Properties['maxFileBytes']) {
|
|
565
|
+
$v = 0
|
|
566
|
+
if ([int64]::TryParse([string]$j.pluginData.maxFileBytes, [ref]$v) -and $v -gt 0) { $pd.maxFileBytes = [Math]::Min([int64]$v, [int64]$SyncExtraHardMaxBytes) }
|
|
567
|
+
}
|
|
568
|
+
# 排除名单是"整份替换",不是追加:用户在面板里删掉一条就真的不再排除。
|
|
569
|
+
# 注意空数组也要能生效(@() 在 PowerShell 里转布尔是 $false,所以按"属性存在"判断)
|
|
570
|
+
if ($j.pluginData.PSObject.Properties['exclude']) { $pd.exclude = @($j.pluginData.exclude | Where-Object { $_ }) }
|
|
571
|
+
if ($j.pluginData.PSObject.Properties['skipDirs']) { $pd.skipDirs = @($j.pluginData.skipDirs | Where-Object { $_ }) }
|
|
572
|
+
}
|
|
573
|
+
if ($j.extra) {
|
|
574
|
+
if ($null -ne $j.extra.PSObject.Properties['enabled']) { $ex.enabled = ($j.extra.enabled -ne $false) }
|
|
575
|
+
if ($j.extra.PSObject.Properties['paths'] -and $j.extra.paths) {
|
|
576
|
+
$ex.paths = @($j.extra.paths | Where-Object { $_ -and $_.id -and $_.src } | ForEach-Object {
|
|
577
|
+
[pscustomobject]@{ id = [string]$_.id; src = [string]$_.src; label = [string]$(if ($_.label) { $_.label } else { $_.id }) }
|
|
578
|
+
})
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
} catch {
|
|
582
|
+
Warn ("config-sync-extra.json 解析失败,按默认规则处理:" + $_.Exception.Message)
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
return [pscustomobject]@{ version = 1; pluginData = $pd; extra = $ex }
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function Save-SyncExtraConfig([string]$DshHome, $cfg) {
|
|
589
|
+
$j = [ordered]@{
|
|
590
|
+
version = 1
|
|
591
|
+
pluginData = [ordered]@{
|
|
592
|
+
enabled = ($cfg.pluginData.enabled -ne $false)
|
|
593
|
+
maxFileBytes = [int64]$cfg.pluginData.maxFileBytes
|
|
594
|
+
exclude = @($cfg.pluginData.exclude | Where-Object { $_ })
|
|
595
|
+
skipDirs = @($cfg.pluginData.skipDirs | Where-Object { $_ })
|
|
596
|
+
}
|
|
597
|
+
extra = [ordered]@{
|
|
598
|
+
enabled = ($cfg.extra.enabled -ne $false)
|
|
599
|
+
paths = @($cfg.extra.paths | Where-Object { $_ } | ForEach-Object { [ordered]@{ id = [string]$_.id; src = [string]$_.src; label = [string]$_.label } })
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
$f = Get-SyncExtraPath $DshHome
|
|
603
|
+
[IO.File]::WriteAllText($f, ($j | ConvertTo-Json -Depth 6), (New-Object Text.UTF8Encoding($false)))
|
|
604
|
+
return $f
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
# 排除判定:文件名或任一路径段命中通配就排除;超过单文件上限也排除(返回原因,空串表示通过)
|
|
608
|
+
function Test-SyncExtraSkip([string]$relName, [long]$size, $cfg) {
|
|
609
|
+
$max = [Math]::Min([int64]$cfg.pluginData.maxFileBytes, [int64]$SyncExtraHardMaxBytes)
|
|
610
|
+
if ($size -gt $max) { return 'too-big' }
|
|
611
|
+
if (-not $relName) { return '' }
|
|
612
|
+
$parts = @($relName -split '[\\/]' | Where-Object { $_ })
|
|
613
|
+
$name = if ($parts.Count -gt 0) { $parts[$parts.Count - 1] } else { $relName }
|
|
614
|
+
$pats = @($cfg.pluginData.exclude)
|
|
615
|
+
foreach ($p in $pats) {
|
|
616
|
+
$wp = New-Object System.Management.Automation.WildcardPattern($p, [System.Management.Automation.WildcardOptions]::IgnoreCase)
|
|
617
|
+
if ($wp.IsMatch($name)) { return 'excluded' }
|
|
618
|
+
foreach ($seg in $parts) { if ($wp.IsMatch($seg)) { return 'excluded' } }
|
|
619
|
+
}
|
|
620
|
+
return ''
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
# 插件数据槽:<DshHome> 一级目录(跳过系统/已覆盖目录与隐藏目录)
|
|
624
|
+
function Get-PluginDataSlots([string]$DshHome, $cfg) {
|
|
625
|
+
$out = New-Object System.Collections.ArrayList
|
|
626
|
+
if (-not $DshHome -or -not (Test-Path -LiteralPath $DshHome)) { return @() }
|
|
627
|
+
$skip = @($cfg.pluginData.skipDirs)
|
|
628
|
+
foreach ($d in (Get-ChildItem -LiteralPath $DshHome -Directory -Force -ErrorAction SilentlyContinue)) {
|
|
629
|
+
$name = $d.Name
|
|
630
|
+
if (-not $name) { continue }
|
|
631
|
+
if ($skip -contains $name) { continue }
|
|
632
|
+
if ($name.StartsWith('.')) { continue }
|
|
633
|
+
[void]$out.Add([pscustomobject]@{ id = $name; label = $name; src = $d.FullName })
|
|
634
|
+
}
|
|
635
|
+
return @($out)
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
# 递归枚举一个槽里「规则允许」的文件(depth 上限 8、条数上限 4000,避免面板读一次就卡住)
|
|
639
|
+
function Get-PluginDataFiles([string]$slotDir, $cfg, [string]$baseDir = '') {
|
|
640
|
+
$files = New-Object System.Collections.ArrayList
|
|
641
|
+
$limit = 4000
|
|
642
|
+
if (-not $baseDir) { $baseDir = $slotDir }
|
|
643
|
+
function Get-FilesInner([string]$dir, [int]$depth) {
|
|
644
|
+
if ($depth -gt 8 -or $files.Count -ge $limit) { return }
|
|
645
|
+
foreach ($f in (Get-ChildItem -LiteralPath $dir -File -Force -ErrorAction SilentlyContinue)) {
|
|
646
|
+
if ($files.Count -ge $limit) { return }
|
|
647
|
+
$rel = Get-RelPath $baseDir $f.FullName
|
|
648
|
+
if (Test-SyncExtraSkip $rel ([long]$f.Length) $cfg) { continue }
|
|
649
|
+
if (Test-SyncExtraSecret $f.FullName) { [void]$script:SyncExtraSecretSkips.Add($rel); continue }
|
|
650
|
+
[void]$files.Add([pscustomobject]@{ rel = $rel; full = $f.FullName; size = [long]$f.Length })
|
|
651
|
+
}
|
|
652
|
+
foreach ($sub in (Get-ChildItem -LiteralPath $dir -Directory -Force -ErrorAction SilentlyContinue)) {
|
|
653
|
+
if ($files.Count -ge $limit) { return }
|
|
654
|
+
# junction / 符号链接一律不跟:pocs、imports 这类目录常是指到别处(甚至别的盘)的链接,
|
|
655
|
+
# 跟进会把几千个外部文件当成"插件数据"抓走,还会一路撞到 4000 上限被截断
|
|
656
|
+
if ($sub.Attributes -band [IO.FileAttributes]::ReparsePoint) { continue }
|
|
657
|
+
if (Test-SyncExtraSkip $sub.Name 0 $cfg) {
|
|
658
|
+
# 目录段命中排除名单时不再下去(node_modules / .git / *cache* 这类)
|
|
659
|
+
$pats = @($cfg.pluginData.exclude)
|
|
660
|
+
$hit = $false
|
|
661
|
+
foreach ($p in $pats) {
|
|
662
|
+
$wp = New-Object System.Management.Automation.WildcardPattern($p, [System.Management.Automation.WildcardOptions]::IgnoreCase)
|
|
663
|
+
if ($wp.IsMatch($sub.Name)) { $hit = $true; break }
|
|
664
|
+
}
|
|
665
|
+
if ($hit) { continue }
|
|
666
|
+
}
|
|
667
|
+
Get-FilesInner $sub.FullName ($depth + 1)
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
Get-FilesInner $slotDir 0
|
|
671
|
+
return @($files)
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
# 必需组:不许取消勾选。理由都是"不勾就报错"这一个事实:
|
|
675
|
+
# profile 里的 package.json / cordis.patch.yml 是 apply 的前置条件 —— 仓库缺它们,apply.ps1 直接 Die;
|
|
676
|
+
# 同一组的 host-contract.json 是两端版本一致性校验,丢了它版本不一致也照铺,客户端可能起不来。
|
|
677
|
+
# 所以这里(以及面板)都把 profile 钉死为开启,手改 JSON 关掉也按开启处理。
|
|
678
|
+
$SyncRequiredGroups = @('profile')
|
|
679
|
+
|
|
680
|
+
function Test-SyncRequiredGroup([string]$Group) { return ($SyncRequiredGroups -contains $Group) }
|
|
681
|
+
|
|
682
|
+
function Get-SyncScopePath([string]$DshHome) { return (Join-Path $DshHome 'config-sync-scope.json') }
|
|
683
|
+
|
|
684
|
+
function Get-SyncScope([string]$DshHome) {
|
|
685
|
+
$path = Get-SyncScopePath $DshHome
|
|
686
|
+
$fallback = [pscustomobject]@{ version = 1; deleteCloud = $false; groups = [pscustomobject]@{} }
|
|
687
|
+
if (-not $path -or -not (Test-Path -LiteralPath $path)) { return $fallback }
|
|
688
|
+
try {
|
|
689
|
+
$raw = Get-Content -LiteralPath $path -Raw -Encoding utf8
|
|
690
|
+
if (-not $raw -or -not $raw.Trim()) { return $fallback }
|
|
691
|
+
$obj = $raw | ConvertFrom-Json
|
|
692
|
+
if ($null -eq $obj) { return $fallback }
|
|
693
|
+
if ($null -eq $obj.PSObject.Properties['deleteCloud']) {
|
|
694
|
+
$obj | Add-Member -NotePropertyName deleteCloud -NotePropertyValue $false -Force
|
|
695
|
+
}
|
|
696
|
+
if ($null -eq $obj.PSObject.Properties['groups']) {
|
|
697
|
+
$obj | Add-Member -NotePropertyName groups -NotePropertyValue ([pscustomobject]@{}) -Force
|
|
698
|
+
}
|
|
699
|
+
return $obj
|
|
700
|
+
} catch {
|
|
701
|
+
Warn ("config-sync-scope.json 解析失败,按「全勾」处理:" + $_.Exception.Message)
|
|
702
|
+
return $fallback
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
function Get-SyncScopeGroup($Scope, [string]$Group) {
|
|
707
|
+
if (-not $Scope -or -not $Scope.groups) { return $null }
|
|
708
|
+
$p = $Scope.groups.PSObject.Properties[$Group]
|
|
709
|
+
if ($null -eq $p) { return $null }
|
|
710
|
+
return $p.Value
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
function Test-SyncGroupEnabled($Scope, [string]$Group) {
|
|
714
|
+
# 必需组永远为真:否则取消勾选 → 仓库缺 profile → 另一台 apply 直接 Die
|
|
715
|
+
if (Test-SyncRequiredGroup $Group) { return $true }
|
|
716
|
+
$g = Get-SyncScopeGroup $Scope $Group
|
|
717
|
+
if ($null -eq $g) { return $true }
|
|
718
|
+
$p = $g.PSObject.Properties['enabled']
|
|
719
|
+
if ($null -ne $p -and $p.Value -eq $false) { return $false }
|
|
720
|
+
return $true
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
function Test-SyncItemEnabled($Scope, [string]$Group, [string]$Item) {
|
|
724
|
+
if (Test-SyncRequiredGroup $Group) { return $true }
|
|
725
|
+
if (-not (Test-SyncGroupEnabled $Scope $Group)) { return $false }
|
|
726
|
+
$g = Get-SyncScopeGroup $Scope $Group
|
|
727
|
+
if ($null -eq $g) { return $true }
|
|
728
|
+
$p = $g.PSObject.Properties['excluded']
|
|
729
|
+
if ($null -eq $p) { return $true }
|
|
730
|
+
return (-not (@($p.Value) -contains $Item))
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
function Test-SyncDeleteCloud($Scope) {
|
|
734
|
+
if (-not $Scope) { return $false }
|
|
735
|
+
return ($Scope.deleteCloud -eq $true)
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
# 整组被跳过时调用:deleteCloud 打开就把仓库里那份删掉;默认什么都不做(留着,以后勾回来接着用)
|
|
739
|
+
function Skip-SyncGroup($Scope, [string]$RepoRoot, [string]$Group) {
|
|
740
|
+
Warn ("跳过(未勾选同步):" + $Group)
|
|
741
|
+
if (-not (Test-SyncDeleteCloud $Scope)) { return }
|
|
742
|
+
$target = Join-Path $RepoRoot $Group
|
|
743
|
+
if (Test-Path -LiteralPath $target) {
|
|
744
|
+
Remove-Item -LiteralPath $target -Recurse -Force
|
|
745
|
+
Warn ("已按「删除云端副本」移除仓库里的 " + $Group)
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
# 组内单项被跳过时调用(item 是仓库里的相对路径,如 skills\foo、settings\x.json)
|
|
750
|
+
function Skip-SyncItem($Scope, [string]$RepoRoot, [string]$Group, [string]$Item, [string]$RepoRel) {
|
|
751
|
+
if (-not (Test-SyncDeleteCloud $Scope) -or -not $RepoRel) { return }
|
|
752
|
+
$target = Join-Path $RepoRoot $RepoRel
|
|
753
|
+
if (Test-Path -LiteralPath $target) {
|
|
754
|
+
Remove-Item -LiteralPath $target -Recurse -Force
|
|
755
|
+
Warn ("已按「删除云端副本」移除仓库里的 " + $RepoRel)
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
# ============================================================
|
|
760
|
+
# 内置运行时链接修复
|
|
761
|
+
# -----------------------------------------------------------
|
|
762
|
+
# 现象:插件页点「卸载」或装新插件时报
|
|
763
|
+
# Cannot find module '<AppRoot>\resources\runtime\primary-runtime\dependencies\pnpm\bin\pnpm.mjs'
|
|
764
|
+
# 原因:客户端把内置运行时实际放在 <DshHome>\dsh-runtimes\dsh-primary-runtime,
|
|
765
|
+
# 而装卸插件时的 pnpm 调用拼的却是安装目录下的 resources\runtime\primary-runtime。
|
|
766
|
+
# 客户端升级会重建 resources\runtime(清空它),链接就此消失,之后所有
|
|
767
|
+
# link: 型插件的装卸都会失败(插件本体没坏,是 pnpm 找不到)。
|
|
768
|
+
# 做法:每次 apply 幂等补回一次 junction。只建、不删、不覆盖已有真实目录。
|
|
769
|
+
# ============================================================
|
|
770
|
+
function Repair-PrimaryRuntimeLink([string]$appRoot, [string]$dshHome) {
|
|
771
|
+
if (-not $appRoot -or -not $dshHome) { return }
|
|
772
|
+
$target = Join-Path $dshHome 'dsh-runtimes\dsh-primary-runtime'
|
|
773
|
+
$targetProbe = Join-Path $target 'dependencies\pnpm\bin\pnpm.mjs'
|
|
774
|
+
$link = Join-Path $appRoot 'resources\runtime\primary-runtime'
|
|
775
|
+
$linkProbe = Join-Path $link 'dependencies\pnpm\bin\pnpm.mjs'
|
|
776
|
+
|
|
777
|
+
# 判据只认「能不能解析出 pnpm.mjs」,不比路径字符串:
|
|
778
|
+
# $dshHome 可能传成 8.3 短名(如 ADMINI~1),而 junction 的 Target 是长名,
|
|
779
|
+
# 字符串一比就误判成「指向别处」,白报一次告警。
|
|
780
|
+
if (Test-Path -LiteralPath $link) {
|
|
781
|
+
if (Test-Path -LiteralPath $linkProbe) { Ok '内置运行时链接已就绪'; return }
|
|
782
|
+
$item = Get-Item -LiteralPath $link -Force
|
|
783
|
+
if ($item.LinkType -and $item.Target) {
|
|
784
|
+
Warn ("内置运行时链接在,但取不到 pnpm.mjs(指向 " + [string](@($item.Target)[0]) + ");本次不动,需人工确认")
|
|
785
|
+
} else {
|
|
786
|
+
Warn ("客户端自带运行时目录存在,但里面没有 pnpm.mjs(" + $link + ");本次不动,若装卸插件失败请检查客户端安装是否完整")
|
|
787
|
+
}
|
|
788
|
+
return
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
# 探针必须落到 pnpm.mjs 本身:只看目录存在会误判「运行时齐了」,而客户端真正 require 的是这个文件
|
|
792
|
+
if (-not (Test-Path -LiteralPath $targetProbe)) {
|
|
793
|
+
Warn ("本机没有可用的内置运行时(" + $target + "),跳过链接修复;插件装卸仍会失败,先让客户端把运行时装好")
|
|
794
|
+
return
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
$parent = Split-Path -Parent $link
|
|
798
|
+
if (-not (Test-Path -LiteralPath $parent)) {
|
|
799
|
+
Warn ("缺少 " + $parent + "(客户端目录结构有变?),跳过运行时链接修复")
|
|
800
|
+
return
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
try {
|
|
804
|
+
New-Item -ItemType Junction -Path $link -Target $target -ErrorAction Stop | Out-Null
|
|
805
|
+
Ok ("已补回内置运行时链接:" + $link + " -> " + $target)
|
|
806
|
+
} catch {
|
|
807
|
+
Warn ("补内置运行时链接失败:" + $_.Exception.Message + ";可手动执行:cmd /c mklink /J `"" + $link + "`" `"" + $target + "`"")
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
# ---------- 远端地址 → 主机名 / 认证自检提示(0.3.27)----------
|
|
812
|
+
<#
|
|
813
|
+
插件不挑平台:远端地址只校验「写法」(git@host:path / ssh:// / https://),
|
|
814
|
+
Gitee、GitHub、GitLab、自建 Gitea 走的是同一套 fetch/push/pull。
|
|
815
|
+
这两个函数只解决「报错文案」:以前自检提示写死 ssh -T git@gitee.com,
|
|
816
|
+
用 GitHub/GitLab 的人照着敲会连到错的主机;现在按 origin 的真实主机名现算。
|
|
817
|
+
#>
|
|
818
|
+
function Get-RemoteHost([string]$Url) {
|
|
819
|
+
if ([string]::IsNullOrWhiteSpace($Url)) { return '' }
|
|
820
|
+
$u = $Url.Trim()
|
|
821
|
+
if ($u -match '^ssh://(?:[^@/]+@)?([^/:]+)') { return $Matches[1] } # ssh://git@host:22/owner/repo.git
|
|
822
|
+
if ($u -match '^[^@/\s]+@([^:/\s]+):') { return $Matches[1] } # git@host:owner/repo.git
|
|
823
|
+
if ($u -match '^https?://([^/]+)') { return $Matches[1] } # https://host/owner/repo.git
|
|
824
|
+
return ''
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
<#
|
|
828
|
+
HTTPS 地址没有 SSH 自检这一说:凭据走 Git 凭据管理器(GCM)或 URL 里的 token;
|
|
829
|
+
只有 SSH 地址才给 ssh -T git@<host>。
|
|
830
|
+
#>
|
|
831
|
+
function Get-RemoteAuthHint([string]$Url) {
|
|
832
|
+
$hostName = Get-RemoteHost $Url
|
|
833
|
+
if ($Url -match '^https?://') {
|
|
834
|
+
if ($hostName) { return ("先确认 " + $hostName + " 的 HTTPS 凭据(Git 凭据管理器里的账号/私人令牌):git ls-remote --heads origin") }
|
|
835
|
+
return '先确认 HTTPS 凭据(Git 凭据管理器里的账号/私人令牌):git ls-remote --heads origin'
|
|
836
|
+
}
|
|
837
|
+
if ($hostName) { return ("先自检 SSH:ssh -T git@" + $hostName) }
|
|
838
|
+
return '先自检 SSH:用 origin 里的真实主机名(ssh -T git@<host>)'
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
# ---------- 跨机路径根(0.3.30 起) ----------
|
|
842
|
+
# 两台机器的客户端装在哪、.dsh 在哪、工作区根在哪,通常都不一样。仓库里凡是这些根下面的
|
|
843
|
+
# 绝对路径一律存成占位符,落地时再展开成本机值:
|
|
844
|
+
# {{APP_ROOT}} 客户端安装目录
|
|
845
|
+
# {{DSH_HOME}} ~\.dsh
|
|
846
|
+
# {{WORKSPACE_HOME}} 工作区根
|
|
847
|
+
# {{ROOT:<key>}} 用户自选目录(ComfyUI 模型目录、mihomo 之类),键值由用户在面板里确认一次
|
|
848
|
+
# 仓库里一律用正斜杠({{APP_ROOT}}/xxx),JSON 里不会出现 \ 转义问题,PowerShell / Node 也都认。
|
|
849
|
+
function Get-SyncRootsPath([string]$DshHome) { return (Join-Path $DshHome 'config-sync-roots.json') }
|
|
850
|
+
function Get-PendingPathsPath([string]$DshHome) { return (Join-Path $DshHome 'config-sync-pending-paths.json') }
|
|
851
|
+
|
|
852
|
+
function ConvertTo-FwdPath([string]$p) {
|
|
853
|
+
if (-not $p) { return '' }
|
|
854
|
+
return $p.Replace('\', '/')
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
# 同一个路径在文本里可能出现成 4 种写法(原样 / JSON 双反斜杠 / 正斜杠 / JSON 转义斜杠)
|
|
858
|
+
function Get-PathVariants([string]$p) {
|
|
859
|
+
if (-not $p) { return @() }
|
|
860
|
+
$bs = [string][char]92
|
|
861
|
+
$out = New-Object System.Collections.ArrayList
|
|
862
|
+
[void]$out.Add($p)
|
|
863
|
+
[void]$out.Add($p.Replace($bs, $bs + $bs))
|
|
864
|
+
[void]$out.Add($p.Replace($bs, '/'))
|
|
865
|
+
[void]$out.Add($p.Replace($bs, $bs + '/'))
|
|
866
|
+
return @($out | Where-Object { $_ })
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
function Get-SyncRoots([string]$DshHome, [string]$AppRoot = '', [string]$WorkspaceHome = '') {
|
|
870
|
+
$o = [ordered]@{
|
|
871
|
+
version = 1
|
|
872
|
+
appRoot = ''
|
|
873
|
+
dshHome = $DshHome
|
|
874
|
+
workspaceHome = ''
|
|
875
|
+
custom = [ordered]@{}
|
|
876
|
+
confirmed = @()
|
|
877
|
+
updatedAt = ''
|
|
878
|
+
}
|
|
879
|
+
$f = Get-SyncRootsPath $DshHome
|
|
880
|
+
if (Test-Path -LiteralPath $f) {
|
|
881
|
+
try {
|
|
882
|
+
$j = Get-Content -LiteralPath $f -Raw -Encoding utf8 | ConvertFrom-Json
|
|
883
|
+
if ($j.appRoot) { $o.appRoot = [string]$j.appRoot }
|
|
884
|
+
if ($j.dshHome) { $o.dshHome = [string]$j.dshHome }
|
|
885
|
+
if ($j.workspaceHome) { $o.workspaceHome = [string]$j.workspaceHome }
|
|
886
|
+
if ($j.custom) { foreach ($k in @($j.custom.PSObject.Properties.Name | Where-Object { $_ })) { $o.custom[$k] = [string]$j.custom.$k } }
|
|
887
|
+
if ($j.confirmed) { $o.confirmed = @($j.confirmed) }
|
|
888
|
+
$o.updatedAt = [string]$j.updatedAt
|
|
889
|
+
} catch { }
|
|
890
|
+
}
|
|
891
|
+
# roots.json 是用户确认过的值,优先;没有才用本次探测/传参
|
|
892
|
+
if (-not $o.appRoot -and $AppRoot) { $o.appRoot = $AppRoot }
|
|
893
|
+
if (-not $o.workspaceHome -and $WorkspaceHome) { $o.workspaceHome = $WorkspaceHome }
|
|
894
|
+
# 用户主目录也能自动探测,不用问人:有些配置把工作区建在 Documents\deepseek-harness 下,
|
|
895
|
+
# 那个路径既不在安装目录也不在 .dsh / 工作区根里,不归一就会把老机器的用户名带过去。
|
|
896
|
+
if (-not $o.custom.Contains('userHome')) { $o.custom['userHome'] = [string]$env:USERPROFILE }
|
|
897
|
+
return [pscustomobject]$o
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
function Save-SyncRoots([string]$DshHome, $roots) {
|
|
901
|
+
$roots.updatedAt = (Get-Date).ToString('s')
|
|
902
|
+
$f = Get-SyncRootsPath $DshHome
|
|
903
|
+
$j = [ordered]@{
|
|
904
|
+
version = 1
|
|
905
|
+
appRoot = [string]$roots.appRoot
|
|
906
|
+
dshHome = [string]$roots.dshHome
|
|
907
|
+
workspaceHome = [string]$roots.workspaceHome
|
|
908
|
+
custom = $(if ($roots.custom) { $roots.custom } else { [ordered]@{} })
|
|
909
|
+
confirmed = @($roots.confirmed)
|
|
910
|
+
updatedAt = $roots.updatedAt
|
|
911
|
+
}
|
|
912
|
+
[IO.File]::WriteAllText($f, ($j | ConvertTo-Json -Depth 6), (New-Object Text.UTF8Encoding($false)))
|
|
913
|
+
return $f
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
# 本机路径 → 占位符(capture 用)。长根先替换,避免根互相包含时替换错位。
|
|
917
|
+
function ConvertTo-TokenText([string]$text, $roots) {
|
|
918
|
+
if (-not $text) { return $text }
|
|
919
|
+
$items = New-Object System.Collections.ArrayList
|
|
920
|
+
if ($roots.appRoot -and $roots.appRoot.Length -ge 3) { [void]$items.Add(@{ Token = '{{APP_ROOT}}'; Value = [string]$roots.appRoot }) }
|
|
921
|
+
if ($roots.workspaceHome -and $roots.workspaceHome.Length -ge 5) { [void]$items.Add(@{ Token = '{{WORKSPACE_HOME}}'; Value = [string]$roots.workspaceHome }) }
|
|
922
|
+
if ($roots.dshHome -and $roots.dshHome.Length -ge 5) { [void]$items.Add(@{ Token = '{{DSH_HOME}}'; Value = [string]$roots.dshHome }) }
|
|
923
|
+
if ($roots.custom) {
|
|
924
|
+
if ($roots.custom -is [System.Collections.IDictionary]) {
|
|
925
|
+
# capture 传进来的 custom 是有序字典(键就是真实的 userHome 之类),不能走 PSObject.Properties
|
|
926
|
+
foreach ($k in @($roots.custom.Keys)) {
|
|
927
|
+
$v = [string]($roots.custom[$k])
|
|
928
|
+
if ($k -and $v -and $v.Length -ge 3) { [void]$items.Add(@{ Token = ('{{ROOT:' + $k + '}}'); Value = $v }) }
|
|
929
|
+
}
|
|
930
|
+
} else {
|
|
931
|
+
foreach ($k in @($roots.custom.PSObject.Properties.Name | Where-Object { $_ })) {
|
|
932
|
+
$v = [string]($roots.custom.PSObject.Properties[$k].Value)
|
|
933
|
+
if ($v -and $v.Length -ge 3) { [void]$items.Add(@{ Token = ('{{ROOT:' + $k + '}}'); Value = $v }) }
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
$sorted = @($items | Sort-Object { $_.Value.Length } -Descending)
|
|
938
|
+
foreach ($r in $sorted) {
|
|
939
|
+
foreach ($v in (Get-PathVariants $r.Value)) { $text = $text.Replace($v, $r.Token) }
|
|
940
|
+
}
|
|
941
|
+
# 占位符后面那一段路径的分隔符统一成正斜杠:{{APP_ROOT}}\x、{{APP_ROOT}}\\x\\y 都变成 {{APP_ROOT}}/x、/y。
|
|
942
|
+
# 只动紧跟在占位符后面的那一段(到引号/逗号/空白为止),不碰 JSON 里别的字符串。
|
|
943
|
+
$text = [regex]::Replace($text, '(\{\{(?:APP_ROOT|DSH_HOME|WORKSPACE_HOME|ROOT:[A-Za-z0-9_.\-]+)\}\})((?:\\\\|\\|/)[^"\s,;:}{\[\]]*)', { param($m) $m.Groups[1].Value + ($m.Groups[2].Value.Replace('\\', '/').Replace('\', '/')) })
|
|
944
|
+
# 自选目录的占位符没配值时会原样留着,交给 apply 报 pending
|
|
945
|
+
return $text
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
# 占位符 → 本机路径(apply 用)。值统一用正斜杠展开。
|
|
949
|
+
function Expand-TokenText([string]$text, $roots) {
|
|
950
|
+
if (-not $text) { return $text }
|
|
951
|
+
$text = $text.Replace('{{APP_ROOT}}', (ConvertTo-FwdPath ([string]$roots.appRoot)))
|
|
952
|
+
$text = $text.Replace('{{WORKSPACE_HOME}}', (ConvertTo-FwdPath ([string]$roots.workspaceHome)))
|
|
953
|
+
$text = $text.Replace('{{DSH_HOME}}', (ConvertTo-FwdPath ([string]$roots.dshHome)))
|
|
954
|
+
if ($roots.custom) {
|
|
955
|
+
if ($roots.custom -is [System.Collections.IDictionary]) {
|
|
956
|
+
foreach ($k in @($roots.custom.Keys)) {
|
|
957
|
+
$v = [string]($roots.custom[$k])
|
|
958
|
+
if ($k -and $v) { $text = $text.Replace(('{{ROOT:' + $k + '}}'), (ConvertTo-FwdPath $v)) }
|
|
959
|
+
}
|
|
960
|
+
} else {
|
|
961
|
+
foreach ($k in @($roots.custom.PSObject.Properties.Name | Where-Object { $_ })) {
|
|
962
|
+
$v = [string]($roots.custom.PSObject.Properties[$k].Value)
|
|
963
|
+
if ($v) { $text = $text.Replace(('{{ROOT:' + $k + '}}'), (ConvertTo-FwdPath $v)) }
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
return $text
|
|
968
|
+
}
|
|
969
|
+
# 文本里还剩哪些没展开的占位符(apply 用;返回去重后的 token 列表)
|
|
970
|
+
function Get-MissingTokens([string]$text) {
|
|
971
|
+
if (-not $text) { return @() }
|
|
972
|
+
$m = [regex]::Matches($text, '\{\{(APP_ROOT|DSH_HOME|WORKSPACE_HOME|ROOT:[A-Za-z0-9_.\-]+)\}\}')
|
|
973
|
+
$set = @{}
|
|
974
|
+
foreach ($x in $m) { $set[$x.Value] = $true }
|
|
975
|
+
return @($set.Keys)
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
function Write-PendingPaths([string]$DshHome, $items) {
|
|
979
|
+
$f = Get-PendingPathsPath $DshHome
|
|
980
|
+
$j = [ordered]@{ generatedAt = (Get-Date).ToString('s'); missing = @($items) }
|
|
981
|
+
[IO.File]::WriteAllText($f, ($j | ConvertTo-Json -Depth 6), (New-Object Text.UTF8Encoding($false)))
|
|
982
|
+
return $f
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
function Clear-PendingPaths([string]$DshHome) {
|
|
986
|
+
$f = Get-PendingPathsPath $DshHome
|
|
987
|
+
if (Test-Path -LiteralPath $f) { Remove-Item -LiteralPath $f -Force -ErrorAction SilentlyContinue }
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
# 占位符给人看的中文说明(面板「路径确认」用,apply 记 pending 时也带上)
|
|
991
|
+
function Get-TokenHint([string]$token) {
|
|
992
|
+
switch ($token) {
|
|
993
|
+
'{{APP_ROOT}}' { return '客户端安装目录(DeepSeekHarness 所在的目录)' }
|
|
994
|
+
'{{DSH_HOME}}' { return '.dsh 配置目录(一般是 %USERPROFILE%\.dsh)' }
|
|
995
|
+
'{{WORKSPACE_HOME}}' { return '工作区根目录(工作区都建在这下面)' }
|
|
996
|
+
default {
|
|
997
|
+
$key = $token -replace '^\{\{ROOT:', ''
|
|
998
|
+
$key = $key -replace '\}\}$', ''
|
|
999
|
+
return ('自选目录:' + $key + ' —— 这台机器上对应的路径由你来定')
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
}
|