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,1188 @@
|
|
|
1
|
+
<#
|
|
2
|
+
DSH 配置同步 —— 抓取脚本(把当前电脑的 DSH 配置收进仓库)
|
|
3
|
+
|
|
4
|
+
方向与 apply.ps1 相反:apply 是「仓库 → 本机」,capture 是「本机 → 仓库」。
|
|
5
|
+
改完插件/技能/预设后,先在本机跑一次 capture,再把仓库同步到另一台机器跑 apply。
|
|
6
|
+
|
|
7
|
+
用法
|
|
8
|
+
pwsh -File capture.ps1 # 默认抓取(配置 + 全部工作区)
|
|
9
|
+
pwsh -File capture.ps1 -AppRoot D:\DSH # 客户端不在 D:\DeepSeekHarness 时必须显式传
|
|
10
|
+
pwsh -File capture.ps1 -PackLocalPlugins # 同时把本地源码插件重新打包进 local-plugins
|
|
11
|
+
pwsh -File capture.ps1 -NoMirrorSkills # 不删除仓库里本机已没有的 Skill(默认会镜像)
|
|
12
|
+
pwsh -File capture.ps1 -WorkspaceOnly # 只抓工作区资料与登记表(不碰配置/插件/预设,无需本机装 DSH)
|
|
13
|
+
pwsh -File capture.ps1 -SkipWorkspace # 跳过工作区同步(应急;默认全量同步所有工作区)
|
|
14
|
+
pwsh -File capture.ps1 -NoMirrorWorkspace # 不删除仓库里本机已没有的工作区/文件(默认镜像删除)
|
|
15
|
+
pwsh -File capture.ps1 -WorkspaceHome "D:\其它\根" # 非客户端目录下工作区根;capture 用它把这类路径归一成 {{WORKSPACE_HOME}}
|
|
16
|
+
|
|
17
|
+
工作区同步(默认开、全量)
|
|
18
|
+
读 $DshHome\storages\workspace.json,把里面登记的**每一个**工作区都收进仓库的 workspace\<title>\ 分槽,
|
|
19
|
+
同时写一份归一化登记表 workspace\workspace-registry.json:路径用 {{APP_ROOT}} / {{WORKSPACE_HOME}} 占位符,
|
|
20
|
+
会话信息(sessionIds / 时间戳 / 归档与置顶会话)一并保留,ID/title 保留。资料用 Copy-Item 字节保真拷贝,
|
|
21
|
+
绝不做 CRLF→LF 归一或 {{APP_ROOT}} 文本替换;工作区里的会话/日志/缓存目录按忽略清单过滤。
|
|
22
|
+
B 机 apply 时按登记表把资料还原到展开后的路径,并按 ID 合并进本机 workspace.json(保留本机自建工作区)。
|
|
23
|
+
|
|
24
|
+
对话/会话数据同步(默认开、全量,随工作区一起;-SkipWorkspace 会连会话一起跳过)
|
|
25
|
+
真正的工作区对话并不在工作区目录里,而在 $DshHome\sessions\、attachments\、
|
|
26
|
+
storages\session_projcache\sessions\、storages\vision_router_session_policy\policies\ 四块,
|
|
27
|
+
收进仓库的 conversations\ 下,全部字节保真(会话日志是 zstd 压缩流,绝不做文本替换)。
|
|
28
|
+
sessions\ 的一级目录名原本是「工作区绝对路径」的编码(如 --D-DeepSeekHarness-Gitee~540C~6B65--),
|
|
29
|
+
两台机器互推就会来回改名,所以仓库里改用**稳定名**:--ws-<工作区ID>--(ID 跨机不变)、im 固定 --im--,
|
|
30
|
+
会话头 header 的 cwd 也归一成 {{APP_ROOT}} / {{WORKSPACE_HOME}} / {{DSH_HOME}} 占位符;
|
|
31
|
+
换机器 apply 时按本机路径重新编码目录名并展开 cwd。目录名 ↔ 工作区ID 的对照仍记在
|
|
32
|
+
conversations\session-index.json 里,旧的路径式目录名会在下次抓取时自动淘汰。
|
|
33
|
+
|
|
34
|
+
建议在 DSH 退出后执行,避免抓到写了一半的配置文件(-WorkspaceOnly 只碰工作区/会话,DSH 开着也能跑)。
|
|
35
|
+
#>
|
|
36
|
+
param(
|
|
37
|
+
[string]$RepoRoot,
|
|
38
|
+
[string]$DshHome = $(Join-Path $env:USERPROFILE '.dsh'),
|
|
39
|
+
[string]$ProfileName = 'desktop',
|
|
40
|
+
[string]$AppRoot = '',
|
|
41
|
+
[switch]$PackLocalPlugins,
|
|
42
|
+
[switch]$NoMirrorSkills,
|
|
43
|
+
[switch]$WorkspaceOnly,
|
|
44
|
+
[switch]$SkipWorkspace,
|
|
45
|
+
[switch]$NoMirrorWorkspace,
|
|
46
|
+
# 文件比较默认走「大小 + mtime 快筛,只在可疑时算哈希」;怀疑仓库有内容漂移时加这个强制全量算哈希
|
|
47
|
+
[switch]$DeepVerify,
|
|
48
|
+
[string]$WorkspaceHome = $(Join-Path $env:USERPROFILE 'Documents\deepseek-harness'),
|
|
49
|
+
# sync.ps1 调起本脚本时必须传:否则本脚本的 Reset 会抹掉父进程已攒下的未同步清单
|
|
50
|
+
[switch]$NoResetNotices
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
$ErrorActionPreference = 'Stop'
|
|
54
|
+
if (-not $RepoRoot) { $RepoRoot = $PSScriptRoot }
|
|
55
|
+
. (Join-Path $PSScriptRoot 'dsh-sync-common.ps1')
|
|
56
|
+
|
|
57
|
+
if (-not $NoResetNotices) { Reset-SyncNotices $DshHome 'capture' }
|
|
58
|
+
|
|
59
|
+
# 客户端可能被卸载重装到别的盘:-AppRoot 先校验,对不上就自动定位(注册表 → 常见位置)。
|
|
60
|
+
$resolvedRoot = Resolve-DshAppRoot $AppRoot
|
|
61
|
+
if (-not $resolvedRoot) {
|
|
62
|
+
Die ("找不到 DSH 客户端目录(传进来的 -AppRoot 对不上,注册表与常见位置也没有)。" +
|
|
63
|
+
"装好客户端后重跑,或显式传 -AppRoot `<客户端根>`(目录里有 resources\app.asar 或 resources\app\package.json)。")
|
|
64
|
+
}
|
|
65
|
+
if ($AppRoot -and ($resolvedRoot -ne $AppRoot.TrimEnd('\', '/'))) { Warn "参数里的 -AppRoot($AppRoot)对不上,已自动改用:$resolvedRoot" }
|
|
66
|
+
$AppRoot = $resolvedRoot
|
|
67
|
+
|
|
68
|
+
# ---------- 工作区资料同步 ----------
|
|
69
|
+
# 用户资料必须字节保真:绝不走 NormalizeTree(CRLF→LF) 或 CopyTracked(占位符文本替换),
|
|
70
|
+
# 一律 Copy-Item -LiteralPath 原样拷;.gitattributes 的 workspace/** -text 让 git 也别改字节。
|
|
71
|
+
# ($BS / $WorkspaceHomeToken / $wsSkip* / Test-WorkspaceIgnored 都在 dsh-sync-common.ps1 里,两个脚本共用)
|
|
72
|
+
|
|
73
|
+
function Get-SlotName([string]$title, [string]$id) {
|
|
74
|
+
$s = $title
|
|
75
|
+
if ([string]::IsNullOrWhiteSpace($s)) { $s = $id }
|
|
76
|
+
$s = ($s -replace '[\\/:*?<>|]', '_').Trim().TrimEnd('.')
|
|
77
|
+
if ([string]::IsNullOrWhiteSpace($s)) { $s = $id }
|
|
78
|
+
return $s
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function ConvertTo-WorkspaceToken([string]$path) {
|
|
82
|
+
# 把本机绝对路径换成占位符前缀:客户端目录下→{{APP_ROOT}},其余→{{WORKSPACE_HOME}}
|
|
83
|
+
$p = $path.Replace($BS,'/')
|
|
84
|
+
$ar = $AppRoot.Replace($BS,'/').TrimEnd('/')
|
|
85
|
+
if ($p -ieq $ar -or $p.StartsWith($ar + '/', [System.StringComparison]::OrdinalIgnoreCase)) {
|
|
86
|
+
return ($AppRootToken + '/' + $p.Substring($ar.Length).TrimStart('/'))
|
|
87
|
+
}
|
|
88
|
+
$wh = $WorkspaceHome.Replace($BS,'/').TrimEnd('/')
|
|
89
|
+
if ($wh -and ($p -ieq $wh -or $p.StartsWith($wh + '/', [System.StringComparison]::OrdinalIgnoreCase))) {
|
|
90
|
+
return ($WorkspaceHomeToken + '/' + $p.Substring($wh.Length).TrimStart('/'))
|
|
91
|
+
}
|
|
92
|
+
# 兜底:既不在 AppRoot 也不在 WorkspaceHome 之下,就把父目录整体当 {{WORKSPACE_HOME}},只留末段
|
|
93
|
+
$leaf = @($p.TrimEnd('/') -split '/')[-1]
|
|
94
|
+
Warn ('工作区路径不在 -AppRoot / -WorkspaceHome 之下,按父目录归一:{0} → {1}/{2}' -f $path, $WorkspaceHomeToken, $leaf)
|
|
95
|
+
return ($WorkspaceHomeToken + '/' + $leaf)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function Sync-WorkspaceCapture {
|
|
99
|
+
$wsDir = Join-Path $RepoRoot 'workspace'
|
|
100
|
+
if (-not (Test-Path -LiteralPath $wsDir)) { New-Item -ItemType Directory -Path $wsDir -Force | Out-Null }
|
|
101
|
+
$registrySrc = Join-Path $DshHome 'storages/workspace.json'
|
|
102
|
+
if (-not (Test-Path -LiteralPath $registrySrc)) {
|
|
103
|
+
Warn ('本机没有 {0},跳过工作区枚举(仓库里已有的 workspace/ 保持不动)' -f $registrySrc)
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
$reg = Get-Content -LiteralPath $registrySrc -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
107
|
+
$table = $reg.tables.workspaces
|
|
108
|
+
$entries = @()
|
|
109
|
+
if ($table) {
|
|
110
|
+
foreach ($prop in $table.PSObject.Properties) {
|
|
111
|
+
$w = $prop.Value
|
|
112
|
+
$title = if ($w.title) { [string]$w.title } else { $prop.Name }
|
|
113
|
+
$entries += [pscustomobject]@{
|
|
114
|
+
Id = $prop.Name
|
|
115
|
+
Title = $title
|
|
116
|
+
LocalPath = [string]$w.path
|
|
117
|
+
Slot = ''
|
|
118
|
+
SessionIds = @($w.sessionIds | ForEach-Object { [string]$_ } | Where-Object { $_ -ne '' })
|
|
119
|
+
ArchivedSessionIds = @($w.archivedSessionIds | ForEach-Object { [string]$_ } | Where-Object { $_ -ne '' })
|
|
120
|
+
PinnedSessionIds = @($w.pinnedSessionIds | ForEach-Object { [string]$_ } | Where-Object { $_ -ne '' })
|
|
121
|
+
CreatedAt = [string]$w.createdAt
|
|
122
|
+
UpdatedAt = [string]$w.updatedAt
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
# 按 ID 排序,保证登记表稳定(反复 capture 不产生假 diff)
|
|
127
|
+
$entries = @($entries | Sort-Object Id)
|
|
128
|
+
$used = @{}
|
|
129
|
+
foreach ($e in $entries) {
|
|
130
|
+
$slot = Get-SlotName $e.Title $e.Id
|
|
131
|
+
if ($used.ContainsKey($slot)) { $slot = Get-SlotName $e.Id $e.Id }
|
|
132
|
+
$n = 1
|
|
133
|
+
while ($used.ContainsKey($slot)) { $slot = (Get-SlotName $e.Title $e.Id) + '-' + $n; $n++ }
|
|
134
|
+
$used[$slot] = $true
|
|
135
|
+
$e.Slot = $slot
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
# 同步内容勾选:未勾选的工作区不抓资料、也不写进登记表(以后勾回来就继续同步)。
|
|
139
|
+
# deleteCloud 打开时顺手把仓库里那一槽删掉;默认什么都不做,留着随时能勾回来。
|
|
140
|
+
$skipWs = @($entries | Where-Object { -not (Test-SyncItemEnabled $Scope 'workspace' $_.Id) })
|
|
141
|
+
foreach ($s in $skipWs) {
|
|
142
|
+
Warn ('跳过(未勾选同步)工作区:{0}' -f $s.Title)
|
|
143
|
+
if (Test-SyncDeleteCloud $Scope) {
|
|
144
|
+
$t = Join-Path $wsDir $s.Slot
|
|
145
|
+
if (Test-Path -LiteralPath $t) {
|
|
146
|
+
Remove-Item -LiteralPath $t -Recurse -Force
|
|
147
|
+
$changed.Add(('workspace/{0}(不同步,已删除云端副本)' -f $s.Slot)) | Out-Null
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
$entries = @($entries | Where-Object { Test-SyncItemEnabled $Scope 'workspace' $_.Id })
|
|
152
|
+
|
|
153
|
+
# 归一化登记表:路径用占位符;会话信息与时间戳一并保留(B 机工作台要能看到历史对话)
|
|
154
|
+
$wsList = @()
|
|
155
|
+
foreach ($e in $entries) {
|
|
156
|
+
$wsList += [ordered]@{
|
|
157
|
+
id = $e.Id
|
|
158
|
+
title = $e.Title
|
|
159
|
+
slot = $e.Slot
|
|
160
|
+
path = (ConvertTo-WorkspaceToken $e.LocalPath)
|
|
161
|
+
sessionIds = @($e.SessionIds)
|
|
162
|
+
archivedSessionIds = @($e.ArchivedSessionIds)
|
|
163
|
+
pinnedSessionIds = @($e.PinnedSessionIds)
|
|
164
|
+
createdAt = $e.CreatedAt
|
|
165
|
+
updatedAt = $e.UpdatedAt
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
$g = $reg.global
|
|
169
|
+
$gDefault = if ($g) { [string]$g.defaultWorkspaceId } else { '' }
|
|
170
|
+
$allIds = @($entries | ForEach-Object { $_.Id })
|
|
171
|
+
$manifest = [ordered]@{}
|
|
172
|
+
$manifest['unit'] = [ordered]@{ name = 'dsh-workspace-sync'; version = 1 }
|
|
173
|
+
$manifest['workspaces'] = @($wsList)
|
|
174
|
+
# 会话 id 集合里不能留 "":$null 过管道会被 [string] 化成空串,DSH 那边不是合法会话 id
|
|
175
|
+
$manifest['global'] = [ordered]@{
|
|
176
|
+
initialized = $true
|
|
177
|
+
defaultWorkspaceId = $gDefault
|
|
178
|
+
workspaceIds = $allIds
|
|
179
|
+
archivedSessionIds = $(if ($g) { @($g.archivedSessionIds | ForEach-Object { [string]$_ } | Where-Object { $_ -ne '' }) } else { @() })
|
|
180
|
+
pinnedSessionIds = $(if ($g) { @($g.pinnedSessionIds | ForEach-Object { [string]$_ } | Where-Object { $_ -ne '' }) } else { @() })
|
|
181
|
+
}
|
|
182
|
+
$manifestText = (($manifest | ConvertTo-Json -Depth 12) -replace "`r`n", "`n") + "`n"
|
|
183
|
+
$manifestPath = Join-Path $wsDir 'workspace-registry.json'
|
|
184
|
+
$old = $null
|
|
185
|
+
if (Test-Path -LiteralPath $manifestPath) { $old = [IO.File]::ReadAllText($manifestPath) -replace "`r`n", "`n" }
|
|
186
|
+
if ($old -cne $manifestText) {
|
|
187
|
+
[IO.File]::WriteAllText($manifestPath, $manifestText, (New-Object Text.UTF8Encoding($false, $false)))
|
|
188
|
+
$changed.Add('workspace/workspace-registry.json') | Out-Null
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
# 逐个同步资料(字节保真)
|
|
192
|
+
$slotSet = @{}
|
|
193
|
+
# 被跳过的工作区,其仓库槽也要占位,避免下面「镜像删除」把它当成“本机已不存在”删掉
|
|
194
|
+
foreach ($s in $skipWs) { $slotSet[$s.Slot] = $true }
|
|
195
|
+
$nAdd = 0; $nDel = 0
|
|
196
|
+
foreach ($e in $entries) {
|
|
197
|
+
$slotSet[$e.Slot] = $true
|
|
198
|
+
$srcRoot = $e.LocalPath
|
|
199
|
+
$dstRoot = Join-Path $wsDir $e.Slot
|
|
200
|
+
if (-not (Test-Path -LiteralPath $srcRoot)) {
|
|
201
|
+
Warn ('工作区「{0}」目录不存在,跳过资料拷贝:{1}' -f $e.Title, $srcRoot)
|
|
202
|
+
continue
|
|
203
|
+
}
|
|
204
|
+
if (-not (Test-Path -LiteralPath $dstRoot)) { New-Item -ItemType Directory -Path $dstRoot -Force | Out-Null }
|
|
205
|
+
$srcFiles = @(Get-ChildItem -LiteralPath $srcRoot -Recurse -File -Force | Where-Object {
|
|
206
|
+
$rel = Get-RelPath $srcRoot $_.FullName
|
|
207
|
+
if (-not $rel) { return $false }
|
|
208
|
+
if (Test-WorkspaceIgnored $rel) { return $false }
|
|
209
|
+
# 超过 100MB 的(模型权重 / whl 安装包等)不进仓库:留在本机,同步只搬配置与资料
|
|
210
|
+
if ($_.Length -gt $wsMaxFileBytes) {
|
|
211
|
+
Warn ('文件过大({0:N1} MB > 100 MB),不参与同步:{1}' -f ($_.Length / 1MB), $rel)
|
|
212
|
+
Add-SyncNotice -Kind 'too-large' -Path $rel -Bytes ([long]$_.Length) -Reason ('{0:N1} MB > 单文件上限 100 MB,未进仓库' -f ($_.Length / 1MB))
|
|
213
|
+
return $false
|
|
214
|
+
}
|
|
215
|
+
return $true
|
|
216
|
+
})
|
|
217
|
+
$srcRels = @{}
|
|
218
|
+
foreach ($f in $srcFiles) { $srcRels[(Get-RelPath $srcRoot $f.FullName)] = $f }
|
|
219
|
+
# 仓库侧也枚举一次:拿 FileInfo 在内存里比对(旧写法每文件两次 Get-FileHash,5 万文件要 2 分钟)
|
|
220
|
+
$dstRels = @{}
|
|
221
|
+
foreach ($rf in @(Get-ChildItem -LiteralPath $dstRoot -Recurse -File -Force)) {
|
|
222
|
+
$rel = Get-RelPath $dstRoot $rf.FullName
|
|
223
|
+
if ($rel) { $dstRels[$rel] = $rf }
|
|
224
|
+
}
|
|
225
|
+
$madeDirs = @{}
|
|
226
|
+
foreach ($rel in @($srcRels.Keys)) {
|
|
227
|
+
$si = $srcRels[$rel]
|
|
228
|
+
if (-not (Test-TreeFileChanged $si $dstRels[$rel])) { continue }
|
|
229
|
+
$d = Join-Path $dstRoot $rel
|
|
230
|
+
$dd = Split-Path -Parent $d
|
|
231
|
+
if (-not $madeDirs.ContainsKey($dd)) {
|
|
232
|
+
if (-not (Test-Path -LiteralPath $dd)) { New-Item -ItemType Directory -Path $dd -Force | Out-Null }
|
|
233
|
+
$madeDirs[$dd] = $true
|
|
234
|
+
}
|
|
235
|
+
Copy-Item -LiteralPath $si.FullName -Destination $d -Force
|
|
236
|
+
$changed.Add(('workspace/{0}/{1}' -f $e.Slot, $rel.Replace($BS,'/'))) | Out-Null
|
|
237
|
+
$nAdd++
|
|
238
|
+
$dstRels[$rel] = [IO.FileInfo]::new($d)
|
|
239
|
+
}
|
|
240
|
+
if (-not $NoMirrorWorkspace -and $srcFiles.Count -gt 0) {
|
|
241
|
+
foreach ($rel in @($dstRels.Keys)) {
|
|
242
|
+
if (-not $srcRels.ContainsKey($rel)) {
|
|
243
|
+
Remove-Item -LiteralPath $dstRels[$rel].FullName -Force
|
|
244
|
+
$changed.Add(('workspace/{0}/{1}(删除)' -f $e.Slot, $rel.Replace($BS,'/'))) | Out-Null
|
|
245
|
+
$nDel++
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
# 镜像删除:仓库里本机已不存在的工作区槽(整目录);空枚举时保护,避免登记表临时读不到就清空
|
|
251
|
+
if (-not $NoMirrorWorkspace -and $entries.Count -gt 0) {
|
|
252
|
+
foreach ($dir in @(Get-ChildItem -LiteralPath $wsDir -Directory -Force)) {
|
|
253
|
+
if (-not $slotSet.ContainsKey($dir.Name)) {
|
|
254
|
+
Remove-Item -LiteralPath $dir.FullName -Recurse -Force
|
|
255
|
+
$changed.Add(('workspace/{0}(删除整个工作区)' -f $dir.Name)) | Out-Null
|
|
256
|
+
Warn ('仓库中移除本机已不存在的工作区:{0}' -f $dir.Name)
|
|
257
|
+
$nDel++
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
Ok ('工作区全量同步:{0} 个工作区,入库/更新 {1} 个文件、删除 {2} 个(字节保真,路径结构不变)' -f $entries.Count, $nAdd, $nDel)
|
|
262
|
+
Say (' ' + (Get-TreeCmpSummary))
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
# ---------- 对话/会话数据同步:常量与函数 ----------
|
|
266
|
+
# 会话日志是 zstd 压缩流、附件是内容寻址 blob、projcache/策略是 JSON,一律字节保真,
|
|
267
|
+
# 绝不做文本替换(zstd 流无法替换;projcache 按 sessionId 索引,cwd 只是历史记录,B 机可读)。
|
|
268
|
+
# sessions\ 一级目录名 = Get-SessionDirName(工作区绝对路径);该编码不可逆,所以 capture 用
|
|
269
|
+
# 「已知工作区路径 → 编码 → 匹配目录名」建 dir ↔ workspaceId 映射,apply 再按本机路径重编码目录名。
|
|
270
|
+
function Get-SessionDirName([string]$path) {
|
|
271
|
+
$sb = New-Object System.Text.StringBuilder
|
|
272
|
+
[void]$sb.Append('--')
|
|
273
|
+
foreach ($ch in $path.ToCharArray()) {
|
|
274
|
+
$c = [int][char]$ch
|
|
275
|
+
if ($c -eq 58) { continue } # ':' 丢弃
|
|
276
|
+
if ($c -eq 92 -or $c -eq 47) { [void]$sb.Append('-'); continue } # \ / → -
|
|
277
|
+
if ($c -lt 128) { [void]$sb.Append($ch) }
|
|
278
|
+
else { [void]$sb.Append(('~{0:X4}' -f $c)) } # 非 ASCII → ~XXXX(连续 CJK 共享 ~ 串联)
|
|
279
|
+
}
|
|
280
|
+
[void]$sb.Append('--')
|
|
281
|
+
return $sb.ToString()
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
<#
|
|
285
|
+
仓库里会话目录用**稳定名**,不用本机路径编码。
|
|
286
|
+
起因:目录名 = 工作区绝对路径编码(--D-DeepSeekHarness-SquadLink--),两台机器各自按自己的路径编码,
|
|
287
|
+
交替 push 时云端目录名来回改名(实测一次 push 11 个 rename),git diff 全变成路径噪音。
|
|
288
|
+
现在:已登记工作区 → --ws-<工作区ID>--(ID 跨机不变),im → --im--,对不上工作区的仍按原名。
|
|
289
|
+
旧仓库里的路径式目录名会被镜像删除自动淘汰(它不是本机现有文件)。
|
|
290
|
+
#>
|
|
291
|
+
function Get-RepoSessionDirName([string]$kind, [string]$id, [string]$localDir) {
|
|
292
|
+
if ($kind -eq 'ws' -and $id) {
|
|
293
|
+
$safe = ($id -replace '[^A-Za-z0-9_.-]', '_')
|
|
294
|
+
if ($safe) { return ('--ws-{0}--' -f $safe) }
|
|
295
|
+
}
|
|
296
|
+
if ($kind -eq 'im') { return '--im--' }
|
|
297
|
+
return $localDir
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function Sync-ConversationCapture {
|
|
301
|
+
$convDir = Join-Path $RepoRoot 'conversations'
|
|
302
|
+
if (-not (Test-Path -LiteralPath $convDir)) { New-Item -ItemType Directory -Path $convDir -Force | Out-Null }
|
|
303
|
+
|
|
304
|
+
# 1) 建 sessions 一级目录名 → workspaceId / im / passthrough 映射
|
|
305
|
+
$wsEncToId = @{}
|
|
306
|
+
$wsPathById = @{}
|
|
307
|
+
$regSrc = Join-Path $DshHome 'storages/workspace.json'
|
|
308
|
+
if (Test-Path -LiteralPath $regSrc) {
|
|
309
|
+
$reg = Get-Content -LiteralPath $regSrc -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
310
|
+
if ($reg.tables.workspaces) {
|
|
311
|
+
foreach ($prop in $reg.tables.workspaces.PSObject.Properties) {
|
|
312
|
+
$wsEncToId[(Get-SessionDirName ([string]$prop.Value.path))] = $prop.Name
|
|
313
|
+
$wsPathById[[string]$prop.Name] = [string]$prop.Value.path
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
$imEnc = Get-SessionDirName (Join-Path $DshHome 'im')
|
|
318
|
+
$sessSrc = Join-Path $DshHome 'sessions'
|
|
319
|
+
# im 目录名 = <home>\im 的编码。短路径(ADMINI~1)时和 DSH 自己写的长名对不上,
|
|
320
|
+
# 所以先精确命中,否则在所有「以 -im-- 收尾」的目录里挑文件最多的那个(当次机器一定在用)。
|
|
321
|
+
$imDirName = ''
|
|
322
|
+
if (Test-Path -LiteralPath $sessSrc) {
|
|
323
|
+
$cands = @(Get-ChildItem -LiteralPath $sessSrc -Directory -Force | Where-Object { $_.Name -eq $imEnc -or $_.Name -like '*-im--' })
|
|
324
|
+
if ($imEnc -and ($cands | Where-Object { $_.Name -eq $imEnc })) { $imDirName = $imEnc }
|
|
325
|
+
elseif ($cands.Count -gt 0) {
|
|
326
|
+
$imDirName = ($cands | Sort-Object { @(Get-ChildItem -LiteralPath $_.FullName -Recurse -File -Force -ErrorAction SilentlyContinue).Count } -Descending | Select-Object -First 1).Name
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
$idxEntries = @()
|
|
330
|
+
$dirRemap = @{} # 本机编码目录名 → 仓库稳定目录名
|
|
331
|
+
$cwdToken = @{} # 仓库稳定目录名 → 归一后的 cwd 占位符(让仓库内容与机器无关)
|
|
332
|
+
$skipSessDirs = @{} # 被“同步内容”取消勾选的工作区目录名(其会话不抓、也不入库)
|
|
333
|
+
if (Test-Path -LiteralPath $sessSrc) {
|
|
334
|
+
foreach ($d in @(Get-ChildItem -LiteralPath $sessSrc -Directory -Force | Sort-Object Name)) {
|
|
335
|
+
if (Test-DefaultWorkspaceDir $d.Name) {
|
|
336
|
+
# 默认工作区不同步(见 dsh-sync-common.ps1 的说明):不记映射,也不入仓库。
|
|
337
|
+
Ok ('默认工作区不参与同步,跳过:{0}' -f $d.Name)
|
|
338
|
+
continue
|
|
339
|
+
}
|
|
340
|
+
if ($wsEncToId.ContainsKey($d.Name) -and -not (Test-SyncItemEnabled $Scope 'conversations' ([string]$wsEncToId[$d.Name]))) {
|
|
341
|
+
$skipSessDirs[$d.Name] = $true
|
|
342
|
+
Warn ('跳过(未勾选同步)该工作区的会话:{0}' -f $d.Name)
|
|
343
|
+
if (Test-SyncDeleteCloud $Scope) {
|
|
344
|
+
$repoDir = Get-RepoSessionDirName 'ws' $wsEncToId[$d.Name] $d.Name
|
|
345
|
+
$t = Join-Path $convDir ('sessions' + [string][char]92 + $repoDir)
|
|
346
|
+
if (Test-Path -LiteralPath $t) {
|
|
347
|
+
Remove-Item -LiteralPath $t -Recurse -Force
|
|
348
|
+
$changed.Add('conversations/sessions/' + $repoDir + '(不同步,已删除云端副本)') | Out-Null
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
continue
|
|
352
|
+
}
|
|
353
|
+
if ($wsEncToId.ContainsKey($d.Name)) {
|
|
354
|
+
$repoDir = Get-RepoSessionDirName 'ws' $wsEncToId[$d.Name] $d.Name
|
|
355
|
+
$dirRemap[$d.Name] = $repoDir
|
|
356
|
+
$idxEntries += [ordered]@{ dir = $repoDir; workspaceId = $wsEncToId[$d.Name] }
|
|
357
|
+
} elseif ($d.Name -eq $imDirName -and $imDirName) {
|
|
358
|
+
$repoDir = Get-RepoSessionDirName 'im' '' $d.Name
|
|
359
|
+
$dirRemap[$d.Name] = $repoDir
|
|
360
|
+
$idxEntries += [ordered]@{ dir = $repoDir; special = 'im' }
|
|
361
|
+
} else {
|
|
362
|
+
$idxEntries += [ordered]@{ dir = $d.Name; passthrough = $true }
|
|
363
|
+
Warn ('会话目录无法对应到已登记工作区,按原名保留(B 机不重编码):{0}' -f $d.Name)
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
# 已登记工作区的 cwd 归一:本机绝对路径 → {{APP_ROOT}}/xxx 这类占位符,apply 时再展开成本机路径。
|
|
368
|
+
# 不归一的话,仓库里这条 cwd 永远是「最后推上来的那台机器」的路径,两台机器互推时每次都改这批文件。
|
|
369
|
+
$imRepoDirName = Get-RepoSessionDirName 'im' '' ''
|
|
370
|
+
$cwdToken[$imRepoDirName] = ($DshHomeToken + '/im')
|
|
371
|
+
foreach ($e in @($idxEntries)) {
|
|
372
|
+
if (-not $e.workspaceId) { continue }
|
|
373
|
+
$localCwd = $null
|
|
374
|
+
foreach ($k in @($wsEncToId.Keys)) { if ($wsEncToId[$k] -eq $e.workspaceId) { $localCwd = $k; break } }
|
|
375
|
+
if (-not $localCwd) { continue }
|
|
376
|
+
$localPath = $null
|
|
377
|
+
if ($wsPathById.ContainsKey([string]$e.workspaceId)) { $localPath = $wsPathById[[string]$e.workspaceId] }
|
|
378
|
+
if ($localPath) { $cwdToken[[string]$e.dir] = (ConvertTo-WorkspaceToken $localPath) }
|
|
379
|
+
}
|
|
380
|
+
$index = [ordered]@{}
|
|
381
|
+
$index['unit'] = [ordered]@{ name = 'dsh-session-sync'; version = 1 }
|
|
382
|
+
$index['entries'] = @($idxEntries)
|
|
383
|
+
$indexText = (($index | ConvertTo-Json -Depth 8) -replace "`r`n", "`n") + "`n"
|
|
384
|
+
$indexPath = Join-Path $convDir 'session-index.json'
|
|
385
|
+
$old = $null
|
|
386
|
+
if (Test-Path -LiteralPath $indexPath) { $old = [IO.File]::ReadAllText($indexPath) -replace "`r`n", "`n" }
|
|
387
|
+
if ($old -cne $indexText) {
|
|
388
|
+
[IO.File]::WriteAllText($indexPath, $indexText, (New-Object Text.UTF8Encoding($false, $false)))
|
|
389
|
+
$changed.Add('conversations/session-index.json') | Out-Null
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
# 2) 四块数据镜像,全部字节保真(SHA256 判差异,Copy-Item 原样拷)
|
|
393
|
+
# sessions 块的相对路径第一段是本机编码目录名,写进仓库前换成稳定名(见 Get-RepoSessionDirName)
|
|
394
|
+
$blocks = @(
|
|
395
|
+
@{ Src = (Join-Path $DshHome 'sessions'); Dst = 'sessions'; Remap = $true },
|
|
396
|
+
@{ Src = (Join-Path $DshHome 'attachments'); Dst = 'attachments'; Remap = $false },
|
|
397
|
+
@{ Src = (Join-Path $DshHome 'storages\session_projcache\sessions'); Dst = 'session_projcache'; Remap = $false },
|
|
398
|
+
@{ Src = (Join-Path $DshHome 'storages\vision_router_session_policy\policies'); Dst = 'vision_router_session_policy'; Remap = $false }
|
|
399
|
+
)
|
|
400
|
+
$nAdd = 0; $nDel = 0
|
|
401
|
+
foreach ($b in $blocks) {
|
|
402
|
+
$dstRoot = Join-Path $convDir $b.Dst
|
|
403
|
+
if (-not (Test-Path -LiteralPath $b.Src)) { Warn ('本机没有会话数据块,跳过:{0}' -f $b.Src); continue }
|
|
404
|
+
if (-not (Test-Path -LiteralPath $dstRoot)) { New-Item -ItemType Directory -Path $dstRoot -Force | Out-Null }
|
|
405
|
+
$srcRels = @{}
|
|
406
|
+
foreach ($f in @(Get-ChildItem -LiteralPath $b.Src -Recurse -File -Force)) {
|
|
407
|
+
$rel = Get-RelPath $b.Src $f.FullName
|
|
408
|
+
if (-not $rel) { continue }
|
|
409
|
+
$parts = @($rel -split '\\')
|
|
410
|
+
# 默认工作区的文件不入库(第一段就是那个 -default-workspace-- 目录名)。
|
|
411
|
+
if (Test-DefaultWorkspaceDir $parts[0]) { continue }
|
|
412
|
+
# 该工作区的会话被取消勾选:整块不抓(目录名保持本机编码,绝不能原样入库)
|
|
413
|
+
if ($b.Remap -and $skipSessDirs.ContainsKey($parts[0])) { continue }
|
|
414
|
+
if ($b.Remap -and $dirRemap.ContainsKey($parts[0])) { $parts[0] = $dirRemap[$parts[0]] }
|
|
415
|
+
$rel = ($parts -join '\')
|
|
416
|
+
$srcRels[$rel] = $f
|
|
417
|
+
}
|
|
418
|
+
$dstRels = @{}
|
|
419
|
+
foreach ($rf in @(Get-ChildItem -LiteralPath $dstRoot -Recurse -File -Force)) {
|
|
420
|
+
$rel = Get-RelPath $dstRoot $rf.FullName
|
|
421
|
+
if ($rel) { $dstRels[$rel] = $rf }
|
|
422
|
+
}
|
|
423
|
+
$madeDirs = @{}
|
|
424
|
+
foreach ($rel in @($srcRels.Keys)) {
|
|
425
|
+
$si = $srcRels[$rel]
|
|
426
|
+
if (-not (Test-TreeFileChanged $si $dstRels[$rel])) { continue }
|
|
427
|
+
$d = Join-Path $dstRoot $rel
|
|
428
|
+
$dd = Split-Path -Parent $d
|
|
429
|
+
if (-not $madeDirs.ContainsKey($dd)) {
|
|
430
|
+
if (-not (Test-Path -LiteralPath $dd)) { New-Item -ItemType Directory -Path $dd -Force | Out-Null }
|
|
431
|
+
$madeDirs[$dd] = $true
|
|
432
|
+
}
|
|
433
|
+
Copy-Item -LiteralPath $si.FullName -Destination $d -Force
|
|
434
|
+
$changed.Add(('conversations/{0}/{1}' -f $b.Dst, $rel.Replace($BS,'/'))) | Out-Null
|
|
435
|
+
$nAdd++
|
|
436
|
+
$dstRels[$rel] = [IO.FileInfo]::new($d)
|
|
437
|
+
}
|
|
438
|
+
# 镜像删除:本机已删的会话文件,仓库里也删
|
|
439
|
+
foreach ($rel in @($dstRels.Keys)) {
|
|
440
|
+
if (-not $srcRels.ContainsKey($rel)) {
|
|
441
|
+
Remove-Item -LiteralPath $dstRels[$rel].FullName -Force
|
|
442
|
+
$changed.Add(('conversations/{0}/{1}(删除)' -f $b.Dst, $rel.Replace($BS,'/'))) | Out-Null
|
|
443
|
+
$nDel++
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
Ok ('会话数据全量同步:{0} 个目录映射,入库/更新 {1} 个文件、删除 {2} 个(四块数据全部字节保真)' -f $idxEntries.Count, $nAdd, $nDel)
|
|
448
|
+
Say (' ' + (Get-TreeCmpSummary))
|
|
449
|
+
|
|
450
|
+
# 3) 仓库副本的会话头 cwd 归一成占位符(只改首帧、幂等),让两台机器推上来的内容完全一致。
|
|
451
|
+
# 对不上工作区的 passthrough 目录不动(那种目录在 B 机本来也认不出)。
|
|
452
|
+
if ($cwdToken.Count -gt 0) {
|
|
453
|
+
$spec = [ordered]@{ home = $convDir; dirs = $cwdToken } | ConvertTo-Json -Depth 4 -Compress
|
|
454
|
+
$specPath = Join-Path ([IO.Path]::GetTempPath()) ('dsh-repo-cwd-{0}.json' -f $stamp)
|
|
455
|
+
[IO.File]::WriteAllText($specPath, $spec, (New-Object Text.UTF8Encoding($false)))
|
|
456
|
+
# 辅助脚本优先用「脚本自己旁边」的那份(仓库可能是个刚 clone 下来、还没铺 tools 的副本)
|
|
457
|
+
$helper = Join-Path $PSScriptRoot 'tools\fix-session-cwd.mjs'
|
|
458
|
+
if (-not (Test-Path -LiteralPath $helper)) { $helper = Join-Path $RepoRoot 'tools\fix-session-cwd.mjs' }
|
|
459
|
+
if (Test-Path -LiteralPath $helper) {
|
|
460
|
+
$nodeExe = Join-Path $AppRoot 'resources\runtime\primary-runtime\dependencies\node\bin\node.exe'
|
|
461
|
+
if (-not (Test-Path -LiteralPath $nodeExe)) { $nodeExe = (Get-Command node -ErrorAction SilentlyContinue).Source }
|
|
462
|
+
& $nodeExe $helper $specPath *>&1 | ForEach-Object { if ($_ -and ([string]$_).Trim()) { Say (' ' + ([string]$_).Trim()) } }
|
|
463
|
+
if ($LASTEXITCODE -ne 0) { Warn '仓库会话头 cwd 归一时有文件失败(上面有原因),那几个会话可能带着本机路径入库' }
|
|
464
|
+
} else {
|
|
465
|
+
Warn ('找不到 {0},跳过仓库会话头归一(仓库里会继续存本机路径)' -f $helper)
|
|
466
|
+
}
|
|
467
|
+
Remove-Item -LiteralPath $specPath -Force -ErrorAction SilentlyContinue
|
|
468
|
+
# 归一改过文件,需要重新统计进 changed 列表,交给调用方统一 git add -A 兜住
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
# ---------- 客户端安装目录的路径归一化 ----------
|
|
473
|
+
# 捕获时把「客户端安装目录」的绝对路径一律写成 {{APP_ROOT}}/ 前缀,apply 时再展开成本机路径。
|
|
474
|
+
# 这是跨机器同步的关键:否则仓库里会同时存在 A 机的 D:\ 和 B 机的 G:\,lockfile 与 package.json 永远对不上。
|
|
475
|
+
#
|
|
476
|
+
# 关键修复(2026-11-10):旧版正则把目录名写死成 DeepSeekHarness,客户端装在 D:\DSH 时归一化整体失效,
|
|
477
|
+
# 本机绝对路径会被原样写进仓库 → 另一台 apply 后 lock 与 package.json 错位 → 安装半装 → 启动崩。
|
|
478
|
+
# 现在先按本机 -AppRoot 的实际路径做字面替换(任意目录名、任意盘符),再用「末级目录名」正则兜底。
|
|
479
|
+
$AppRootPattern = Get-AppRootPattern $AppRoot
|
|
480
|
+
|
|
481
|
+
function Set-AppRootToken([string]$text) {
|
|
482
|
+
return (Convert-AppRootText $text $AppRoot $AppRootToken)
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
Say "== DSH 配置同步 / capture =="
|
|
486
|
+
|
|
487
|
+
# 同步内容勾选(面板「同步内容」卡写在本机 ~\.dsh\config-sync-scope.json)
|
|
488
|
+
$Scope = Get-SyncScope $DshHome
|
|
489
|
+
|
|
490
|
+
$ProfileDir = Join-Path $DshHome ("profiles\" + $ProfileName)
|
|
491
|
+
if (-not $WorkspaceOnly -and -not (Test-Path -LiteralPath $ProfileDir)) { Die "找不到 profile:$ProfileDir" }
|
|
492
|
+
foreach ($d in @('profile', 'prompt', 'skills', 'presets', 'local-plugins', 'storages-record', 'settings', 'mnemon', 'plugin-data', 'extra', 'workspace', 'conversations')) {
|
|
493
|
+
$p = Join-Path $RepoRoot $d
|
|
494
|
+
if (-not (Test-Path -LiteralPath $p)) { New-Item -ItemType Directory -Path $p -Force | Out-Null }
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
$changed = New-Object System.Collections.Generic.List[string]
|
|
498
|
+
|
|
499
|
+
# -WorkspaceOnly:只抓工作区 + 会话(B 机没装 DSH 也能先还原资料),抓完直接给提交指引并结束
|
|
500
|
+
if ($WorkspaceOnly) {
|
|
501
|
+
if ($SkipWorkspace) { Die "-WorkspaceOnly 与 -SkipWorkspace 不能同时使用" }
|
|
502
|
+
if (Test-SyncGroupEnabled $Scope 'workspace') { Sync-WorkspaceCapture } else { Skip-SyncGroup $Scope $RepoRoot 'workspace' }
|
|
503
|
+
if (Test-SyncGroupEnabled $Scope 'conversations') { Sync-ConversationCapture } else { Skip-SyncGroup $Scope $RepoRoot 'conversations' }
|
|
504
|
+
Say ""
|
|
505
|
+
if ($changed.Count -eq 0) {
|
|
506
|
+
Ok "工作区 / 会话与仓库一致,无需提交"
|
|
507
|
+
} else {
|
|
508
|
+
Say "本次有 $($changed.Count) 处变更:"
|
|
509
|
+
$changed | ForEach-Object { Say " - $_" }
|
|
510
|
+
Say ""
|
|
511
|
+
Say "接着执行:"
|
|
512
|
+
Say " cd `"$RepoRoot`""
|
|
513
|
+
Say " git add -A ; git commit -m `"workspace + sessions sync`" ; git push"
|
|
514
|
+
Say "另一台机器:git pull ; powershell -ExecutionPolicy Bypass -File .\apply.ps1 -WorkspaceOnly"
|
|
515
|
+
}
|
|
516
|
+
return
|
|
517
|
+
}
|
|
518
|
+
function Get-NormalizedHash($path) {
|
|
519
|
+
# 文本文件按"去掉 CRLF/LF 差异后的内容"比较,避免换行转换制造假变更;归档文件按原始字节比较
|
|
520
|
+
$ext = [IO.Path]::GetExtension($path).ToLower()
|
|
521
|
+
if ($ext -in @('.tgz', '.zip')) {
|
|
522
|
+
return (Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash
|
|
523
|
+
}
|
|
524
|
+
$bytes = [IO.File]::ReadAllBytes($path)
|
|
525
|
+
$text = [Text.Encoding]::UTF8.GetString($bytes)
|
|
526
|
+
$norm = $text -replace "`r`n", "`n"
|
|
527
|
+
$sha = [Security.Cryptography.SHA256]::Create()
|
|
528
|
+
return [BitConverter]::ToString($sha.ComputeHash([Text.Encoding]::UTF8.GetBytes($norm)))
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
# ---------- 大目录文件比较(性能关键路径) ----------
|
|
532
|
+
# 旧写法对每个文件算两次 Get-FileHash(源 + 仓库),5 万个文件实测约 120s。
|
|
533
|
+
# 现在:两个 tree 各枚举一次拿 FileInfo(Length/LastWriteTimeUtc 已在枚举时读好,不再 stat),
|
|
534
|
+
# 内存里比对;只有「大小不同」或「源比仓库新」才对该文件算哈希确认内容。
|
|
535
|
+
# 这与 git 自己的判定策略一致(size+mtime 变了才重算 blob)。
|
|
536
|
+
$script:CmpDeep = [bool]$DeepVerify
|
|
537
|
+
$script:CmpFileCount = 0
|
|
538
|
+
$script:CmpHashCount = 0
|
|
539
|
+
|
|
540
|
+
function Test-TreeFileChanged($srcInfo, $dstInfo) {
|
|
541
|
+
# $srcInfo / $dstInfo 都是 FileInfo($dstInfo 为 $null 表示仓库里没有这个文件)
|
|
542
|
+
$script:CmpFileCount++
|
|
543
|
+
if ($null -eq $dstInfo) { return $true }
|
|
544
|
+
if ($srcInfo.Length -ne $dstInfo.Length) { return $true }
|
|
545
|
+
if ($script:CmpDeep -or ($srcInfo.LastWriteTimeUtc -gt $dstInfo.LastWriteTimeUtc)) {
|
|
546
|
+
$script:CmpHashCount++
|
|
547
|
+
return ((Get-NormalizedHash $srcInfo.FullName) -ne (Get-NormalizedHash $dstInfo.FullName))
|
|
548
|
+
}
|
|
549
|
+
return $false
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function Get-TreeCmpSummary {
|
|
553
|
+
$deep = if ($script:CmpDeep) { '深校验开' } else { '快筛' }
|
|
554
|
+
return ('文件比较({0}):比对 {1} 个,深哈希 {2} 个' -f $deep, $script:CmpFileCount, $script:CmpHashCount)
|
|
555
|
+
}
|
|
556
|
+
function CopyNormalized($src, $dst) {
|
|
557
|
+
# 二进制原样拷贝;文本统一写成 LF(与 .gitattributes 的 eol=lf 对齐),并保留原文件的 UTF-8 BOM
|
|
558
|
+
$ext = [IO.Path]::GetExtension($src).ToLower()
|
|
559
|
+
if ($ext -in @('.tgz', '.zip', '.png', '.jpg', '.jpeg', '.gif', '.ico', '.exe', '.dll', '.wasm')) {
|
|
560
|
+
Copy-Item -LiteralPath $src -Destination $dst -Force
|
|
561
|
+
return
|
|
562
|
+
}
|
|
563
|
+
$bytes = [IO.File]::ReadAllBytes($src)
|
|
564
|
+
$hasBom = ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF)
|
|
565
|
+
$text = [Text.Encoding]::UTF8.GetString($bytes)
|
|
566
|
+
if ($hasBom -and $text.Length -gt 0 -and $text[0] -eq [char]0xFEFF) { $text = $text.Substring(1) }
|
|
567
|
+
$text = $text -replace "`r`n", "`n"
|
|
568
|
+
$enc = New-Object Text.UTF8Encoding($hasBom, $false)
|
|
569
|
+
[IO.File]::WriteAllText($dst, $text, $enc)
|
|
570
|
+
}
|
|
571
|
+
function Get-RepoText([string]$path) {
|
|
572
|
+
# 仓库里文件的现有文本:只去 BOM、统一 LF,不做占位符替换(用来判断是否真的需要重写)
|
|
573
|
+
$bytes = [IO.File]::ReadAllBytes($path)
|
|
574
|
+
$text = [Text.Encoding]::UTF8.GetString($bytes)
|
|
575
|
+
if ($text.Length -gt 0 -and $text[0] -eq [char]0xFEFF) { $text = $text.Substring(1) }
|
|
576
|
+
return ($text -replace "`r`n", "`n")
|
|
577
|
+
}
|
|
578
|
+
function Get-TrackedText([string]$path) {
|
|
579
|
+
# 取「用于写进仓库的规范文本」:去掉 BOM、CRLF→LF、客户端安装目录→{{APP_ROOT}}
|
|
580
|
+
$bytes = [IO.File]::ReadAllBytes($path)
|
|
581
|
+
$hasBom = ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF)
|
|
582
|
+
$text = [Text.Encoding]::UTF8.GetString($bytes)
|
|
583
|
+
if ($hasBom -and $text.Length -gt 0 -and $text[0] -eq [char]0xFEFF) { $text = $text.Substring(1) }
|
|
584
|
+
$text = $text -replace "`r`n", "`n"
|
|
585
|
+
return @{ Text = (Set-AppRootToken $text); Bom = $hasBom }
|
|
586
|
+
}
|
|
587
|
+
function NormalizeTree($dir) {
|
|
588
|
+
# 目录镜像后调用:把文本文件统一成 LF,并把客户端安装目录换成占位符,避免 git 因换行/盘符判成"有改动"
|
|
589
|
+
if (-not (Test-Path -LiteralPath $dir)) { return }
|
|
590
|
+
Get-ChildItem -LiteralPath $dir -Recurse -File -Force | ForEach-Object {
|
|
591
|
+
$ext = $_.Extension.ToLower()
|
|
592
|
+
if ($ext -in @('.tgz', '.zip', '.png', '.jpg', '.jpeg', '.gif', '.ico', '.exe', '.dll', '.wasm')) { return }
|
|
593
|
+
$norm = Get-TrackedText $_.FullName
|
|
594
|
+
$raw = [Text.Encoding]::UTF8.GetString([IO.File]::ReadAllBytes($_.FullName))
|
|
595
|
+
if ($raw -ceq $norm.Text) { return }
|
|
596
|
+
[IO.File]::WriteAllText($_.FullName, $norm.Text, (New-Object Text.UTF8Encoding($norm.Bom, $false)))
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
function CopyTracked($src, $dst, $label) {
|
|
600
|
+
$ext = [IO.Path]::GetExtension($src).ToLower()
|
|
601
|
+
if ($ext -in @('.tgz', '.zip', '.png', '.jpg', '.jpeg', '.gif', '.ico', '.exe', '.dll', '.wasm')) {
|
|
602
|
+
$isNew = -not (Test-Path -LiteralPath $dst)
|
|
603
|
+
if (-not $isNew -and (Get-NormalizedHash $src) -eq (Get-NormalizedHash $dst)) {
|
|
604
|
+
Say (" = " + $label + "(无变化)")
|
|
605
|
+
return
|
|
606
|
+
}
|
|
607
|
+
CopyNormalized $src $dst
|
|
608
|
+
$changed.Add($label) | Out-Null
|
|
609
|
+
Ok ("更新 " + $label)
|
|
610
|
+
return
|
|
611
|
+
}
|
|
612
|
+
# 文本:和仓库里「原样文本」比较(不能把仓库文本也先做占位符替换,否则盘符残留会被判成"无变化")
|
|
613
|
+
$mine = Get-TrackedText $src
|
|
614
|
+
if (Test-Path -LiteralPath $dst) {
|
|
615
|
+
if ((Get-RepoText $dst) -ceq $mine.Text) {
|
|
616
|
+
Say (" = " + $label + "(无变化)")
|
|
617
|
+
return
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
[IO.File]::WriteAllText($dst, $mine.Text, (New-Object Text.UTF8Encoding($mine.Bom, $false)))
|
|
621
|
+
$changed.Add($label) | Out-Null
|
|
622
|
+
Ok ("更新 " + $label)
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
if (Test-SyncGroupEnabled $Scope 'profile') {
|
|
626
|
+
# 1) profile 配置
|
|
627
|
+
# package.json 里的 link: 指向的是「本机客户端的绝对路径」,入库存成 {{APP_ROOT}}/ 前缀。
|
|
628
|
+
# 这样 A 机 apply 后路径变成本机的、B 机再 capture 也不会把仓库改成自己的盘符,
|
|
629
|
+
# 更关键的是 pnpm-lock.yaml 与 package.json 的 specifier 始终一致,--frozen-lockfile 不会挂。
|
|
630
|
+
$pkgRaw = $null
|
|
631
|
+
$pkgSrc = Join-Path $ProfileDir 'package.json'
|
|
632
|
+
if (Test-Path -LiteralPath $pkgSrc) { $pkgRaw = Set-AppRootToken ([IO.File]::ReadAllText($pkgSrc)) }
|
|
633
|
+
foreach ($dirName in @('dsh-our-free-model-2.8.0', 'dsh-purge-1.1.92')) {
|
|
634
|
+
$pattern = 'link:[^"]*' + [regex]::Escape($dirName)
|
|
635
|
+
$pkgRaw = [regex]::Replace($pkgRaw, $pattern, ('link:' + $AppRootToken + '/' + $dirName), 'IgnoreCase')
|
|
636
|
+
}
|
|
637
|
+
if ($pkgRaw) {
|
|
638
|
+
$norm = $pkgRaw -replace "`r`n", "`n"
|
|
639
|
+
$dst = Join-Path $RepoRoot 'profile\package.json'
|
|
640
|
+
$existing = $null
|
|
641
|
+
if (Test-Path -LiteralPath $dst) { $existing = [IO.File]::ReadAllText($dst) -replace "`r`n", "`n" }
|
|
642
|
+
if ($null -ne $existing -and $existing -eq $norm) {
|
|
643
|
+
Say " = profile\package.json(无变化)"
|
|
644
|
+
} else {
|
|
645
|
+
[IO.File]::WriteAllText($dst, $norm, (New-Object Text.UTF8Encoding($false, $false)))
|
|
646
|
+
$changed.Add('profile\package.json') | Out-Null
|
|
647
|
+
Ok "更新 profile\package.json(link 路径已归一为 $AppRootToken)"
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
foreach ($f in @('pnpm-lock.yaml', 'pnpm-workspace.yaml', 'cordis.yml', 'cordis.patch.yml')) {
|
|
651
|
+
$src = Join-Path $ProfileDir $f
|
|
652
|
+
if (Test-Path -LiteralPath $src) { CopyTracked $src (Join-Path $RepoRoot "profile\$f") "profile\$f" }
|
|
653
|
+
elseif (Test-Path -LiteralPath (Join-Path $RepoRoot "profile\$f")) {
|
|
654
|
+
Warn "本机缺少 profile\$f,仓库里保留的是上一次的副本(该机器 apply 时会把它铺过来)"
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
# 1.5) 宿主版本契约:记下本机实测的 app 版本,另一台 apply 时据此比对,版本不一致就不铺实验性 bundle
|
|
659
|
+
# 只写 appVersion + electronVersion,不写机器名/日期(那两样会让每次 capture 都产生假 diff)。
|
|
660
|
+
# 已存在且版本不同 → 只告警不改写,避免「较旧的那台 capture 一次就把契约拉低」。
|
|
661
|
+
$hostAppVersion = Get-HostAppVersion $AppRoot
|
|
662
|
+
$contractPath = Join-Path $RepoRoot 'profile\host-contract.json'
|
|
663
|
+
if (-not $hostAppVersion) {
|
|
664
|
+
Warn "读不到 app 版本($AppRoot\resources\app\package.json),跳过 host-contract.json"
|
|
665
|
+
} else {
|
|
666
|
+
$contractText = ("{`n `"appVersion`": `"$hostAppVersion`",`n `"electronVersion`": `"$(Get-HostElectronVersion $AppRoot)`"`n}`n")
|
|
667
|
+
$existing = $null
|
|
668
|
+
if (Test-Path -LiteralPath $contractPath) {
|
|
669
|
+
try { $existing = [string](Get-Content -LiteralPath $contractPath -Raw -Encoding utf8 | ConvertFrom-Json).appVersion } catch { $existing = $null }
|
|
670
|
+
}
|
|
671
|
+
if ($existing -and ($existing -ne $hostAppVersion)) {
|
|
672
|
+
Warn "仓库契约 app = $existing,本机 app = $hostAppVersion(不一致,契约保持不变;对齐后再 capture)"
|
|
673
|
+
} else {
|
|
674
|
+
$norm = $contractText -replace "`r`n", "`n"
|
|
675
|
+
$now = if (Test-Path -LiteralPath $contractPath) { ([IO.File]::ReadAllText($contractPath) -replace "`r`n", "`n") } else { '' }
|
|
676
|
+
if ($now -ceq $norm) {
|
|
677
|
+
Say " = profile\host-contract.json(无变化)"
|
|
678
|
+
} else {
|
|
679
|
+
[IO.File]::WriteAllText($contractPath, $norm, (New-Object Text.UTF8Encoding($false, $false)))
|
|
680
|
+
$changed.Add('profile\host-contract.json') | Out-Null
|
|
681
|
+
Ok "写入 profile\host-contract.json(app = $hostAppVersion)"
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
} else { Skip-SyncGroup $Scope $RepoRoot 'profile' }
|
|
687
|
+
|
|
688
|
+
# 2) 全局提示
|
|
689
|
+
if (Test-SyncGroupEnabled $Scope 'prompt') {
|
|
690
|
+
foreach ($f in @('AGENTS.md', 'prompt-inject.md')) {
|
|
691
|
+
if (-not (Test-SyncItemEnabled $Scope 'prompt' $f)) { Skip-SyncItem $Scope $RepoRoot 'prompt' $f ("prompt\" + $f); continue }
|
|
692
|
+
$src = Join-Path $DshHome $f
|
|
693
|
+
if (Test-Path -LiteralPath $src) { CopyTracked $src (Join-Path $RepoRoot "prompt\$f") "prompt\$f" }
|
|
694
|
+
elseif (Test-Path -LiteralPath (Join-Path $RepoRoot "prompt\$f")) {
|
|
695
|
+
Warn "本机缺少 prompt\$f,仓库里保留的是上一次的副本"
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
} else { Skip-SyncGroup $Scope $RepoRoot 'prompt' }
|
|
700
|
+
|
|
701
|
+
# 3) Skills(镜像)
|
|
702
|
+
if (Test-SyncGroupEnabled $Scope 'skills') {
|
|
703
|
+
$skillsSrc = Join-Path $DshHome 'skills'
|
|
704
|
+
$skillsDst = Join-Path $RepoRoot 'skills'
|
|
705
|
+
if (Test-Path -LiteralPath $skillsSrc) {
|
|
706
|
+
Get-ChildItem -LiteralPath $skillsSrc -Directory -Force | ForEach-Object {
|
|
707
|
+
if (-not (Test-SyncItemEnabled $Scope 'skills' $_.Name)) { Skip-SyncItem $Scope $RepoRoot 'skills' $_.Name ("skills\" + $_.Name); return }
|
|
708
|
+
$dstDir = Join-Path $skillsDst $_.Name
|
|
709
|
+
if (-not (Test-Path -LiteralPath $dstDir)) { $changed.Add("skills\$($_.Name)") | Out-Null }
|
|
710
|
+
if (Test-Path -LiteralPath $dstDir) { Remove-Item -LiteralPath $dstDir -Recurse -Force }
|
|
711
|
+
Copy-Item -LiteralPath $_.FullName -Destination $dstDir -Recurse -Force
|
|
712
|
+
NormalizeTree $dstDir
|
|
713
|
+
}
|
|
714
|
+
$count = (Get-ChildItem -LiteralPath $skillsSrc -Directory -Force | Measure-Object).Count
|
|
715
|
+
Ok "已抓取 $count 个 Skill"
|
|
716
|
+
if (-not $NoMirrorSkills) {
|
|
717
|
+
Get-ChildItem -LiteralPath $skillsDst -Directory -Force | Where-Object {
|
|
718
|
+
-not (Test-Path -LiteralPath (Join-Path $skillsSrc $_.Name))
|
|
719
|
+
} | ForEach-Object {
|
|
720
|
+
Remove-Item -LiteralPath $_.FullName -Recurse -Force
|
|
721
|
+
$changed.Add("skills\$($_.Name)(删除)") | Out-Null
|
|
722
|
+
Warn "仓库中移除本机已不存在的 Skill:$($_.Name)"
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
} else { Skip-SyncGroup $Scope $RepoRoot 'skills' }
|
|
728
|
+
|
|
729
|
+
# 4) Agent 预设
|
|
730
|
+
if (Test-SyncGroupEnabled $Scope 'presets') {
|
|
731
|
+
$presetSrc = Join-Path $DshHome '.agent-presets'
|
|
732
|
+
$presetDst = Join-Path $RepoRoot 'presets'
|
|
733
|
+
if (Test-Path -LiteralPath $presetSrc) {
|
|
734
|
+
Get-ChildItem -LiteralPath $presetSrc -Directory -Force | ForEach-Object {
|
|
735
|
+
if (-not (Test-SyncItemEnabled $Scope 'presets' $_.Name)) { Skip-SyncItem $Scope $RepoRoot 'presets' $_.Name ("presets\" + $_.Name); return }
|
|
736
|
+
$dstDir = Join-Path $presetDst $_.Name
|
|
737
|
+
if (Test-Path -LiteralPath $dstDir) { Remove-Item -LiteralPath $dstDir -Recurse -Force }
|
|
738
|
+
Copy-Item -LiteralPath $_.FullName -Destination $dstDir -Recurse -Force
|
|
739
|
+
NormalizeTree $dstDir
|
|
740
|
+
}
|
|
741
|
+
Ok "已抓取 Agent 预设:$((Get-ChildItem -LiteralPath $presetSrc -Directory -Force | Select-Object -ExpandProperty Name) -join ', ')"
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
} else { Skip-SyncGroup $Scope $RepoRoot 'presets' }
|
|
745
|
+
|
|
746
|
+
# 5) 技能市场台账
|
|
747
|
+
if (Test-SyncGroupEnabled $Scope 'storages-record') {
|
|
748
|
+
$dataDir = Join-Path $DshHome 'storages\@montersy123\dsh-skill-market\data'
|
|
749
|
+
foreach ($pair in @(@('installed.json', 'skill-market-installed.json'), @('panel.json', 'skill-market-panel.json'))) {
|
|
750
|
+
if (-not (Test-SyncItemEnabled $Scope 'storages-record' $pair[1])) {
|
|
751
|
+
Skip-SyncItem $Scope $RepoRoot 'storages-record' $pair[1] ("storages-record\" + $pair[1])
|
|
752
|
+
continue
|
|
753
|
+
}
|
|
754
|
+
$src = Join-Path $dataDir $pair[0]
|
|
755
|
+
if (Test-Path -LiteralPath $src) { CopyTracked $src (Join-Path $RepoRoot ("storages-record\" + $pair[1])) ("storages-record\" + $pair[1]) }
|
|
756
|
+
}
|
|
757
|
+
} else { Skip-SyncGroup $Scope $RepoRoot 'storages-record' }
|
|
758
|
+
|
|
759
|
+
# 5.5) 插件设置与工作流(跨机需要一致的那些小配置)
|
|
760
|
+
# 0.3.30 起:① 覆盖到各插件自己的配置(以前只同步 4 个);② 写进仓库前把本机根路径换成
|
|
761
|
+
# {{APP_ROOT}} / {{DSH_HOME}} / {{WORKSPACE_HOME}} / {{ROOT:<key>}} 占位符,
|
|
762
|
+
# 换机器落地时由 apply 展开成本机路径,不会把老机器的盘符带过去。
|
|
763
|
+
if (Test-SyncGroupEnabled $Scope 'settings') {
|
|
764
|
+
# 路径根:第一次跑就落一份(供稳定不变),之后沿用用户确认过的值
|
|
765
|
+
$roots = Get-SyncRoots $DshHome $AppRoot $WorkspaceHome
|
|
766
|
+
# 每次 capture 都把根写回一份:userHome 是自动探测的,写盘后面板才看得到、也才能被用户改掉
|
|
767
|
+
Save-SyncRoots $DshHome $roots | Out-Null
|
|
768
|
+
Ok ('路径根:appRoot=' + $roots.appRoot + ';dshHome=' + $roots.dshHome + ';workspaceHome=' + $roots.workspaceHome + '(已写入 config-sync-roots.json)')
|
|
769
|
+
|
|
770
|
+
$settingsMap = @(
|
|
771
|
+
@{ Repo = 'settings\our-free-model.json'; Src = (Join-Path $DshHome 'our-free-model\settings.json'); Label = 'our-free-model 插件设置' },
|
|
772
|
+
@{ Repo = 'settings\comfyui-workflows.json'; Src = (Join-Path $DshHome 'data\dsh-comfyui\workflows.json'); Label = 'ComfyUI 工作流库' },
|
|
773
|
+
@{ Repo = 'settings\dsh-pet-main-config.jsonc'; Src = (Join-Path $DshHome 'dsh-pet\main-config.jsonc'); Label = '桌宠配置' },
|
|
774
|
+
@{ Repo = 'settings\dsh-market-state.json'; Src = (Join-Path $DshHome ("profiles\" + $ProfileName + '\.dsh-market\state.json')); Label = '插件市场状态' },
|
|
775
|
+
@{ Repo = 'settings\dsh-pet-memory.json'; Src = (Join-Path $DshHome 'dsh-pet\memory.json'); Label = '桌宠记忆' },
|
|
776
|
+
@{ Repo = 'settings\our-free-model-picks.json'; Src = (Join-Path $DshHome 'our-free-model\picks.json'); Label = 'our-free-model 收藏' },
|
|
777
|
+
@{ Repo = 'settings\dsh-im-interface-language.json'; Src = (Join-Path $DshHome 'integrations\dsh-im\interface-language.json'); Label = 'dsh-im 界面语言' },
|
|
778
|
+
@{ Repo = 'settings\dsh-qq-config.json'; Src = (Join-Path $DshHome 'integrations\dsh-qq\config.json'); Label = 'QQ 集成配置' },
|
|
779
|
+
@{ Repo = 'settings\dsh-qq-workspaces.json'; Src = (Join-Path $DshHome 'integrations\dsh-qq\workspaces.json'); Label = 'QQ 集成工作区' },
|
|
780
|
+
@{ Repo = 'settings\redteam-preset-declare.json'; Src = (Join-Path $DshHome 'redteam\preset-declare.json'); Label = '红队预设声明' },
|
|
781
|
+
@{ Repo = 'settings\task-board-ledger.json'; Src = (Join-Path $DshHome 'task-board\ledger-v2.json'); Label = '任务板台账' },
|
|
782
|
+
@{ Repo = 'settings\task-board-scheduler.json'; Src = (Join-Path $DshHome 'task-board\scheduler-v2.json'); Label = '任务板调度' },
|
|
783
|
+
@{ Repo = 'settings\dsh-workspaces.json'; Src = (Join-Path $DshHome 'storages\workspace.json'); Label = 'DSH 工作区登记' }
|
|
784
|
+
)
|
|
785
|
+
foreach ($m in $settingsMap) {
|
|
786
|
+
if (-not (Test-SyncItemEnabled $Scope 'settings' ($m.Repo -replace '^settings\\', ''))) {
|
|
787
|
+
Skip-SyncItem $Scope $RepoRoot 'settings' $m.Repo $m.Repo
|
|
788
|
+
continue
|
|
789
|
+
}
|
|
790
|
+
if (-not (Test-Path -LiteralPath $m.Src)) {
|
|
791
|
+
Warn ("本机没有 " + $m.Label + " 的源文件,跳过:" + $m.Src)
|
|
792
|
+
continue
|
|
793
|
+
}
|
|
794
|
+
$txt = [IO.File]::ReadAllText($m.Src)
|
|
795
|
+
if ($m.Repo -eq 'settings\our-free-model.json') {
|
|
796
|
+
# 这个文件里有插件自己每秒/每轮刷新的运行时字段,原样入库会导致每次 capture 都"有改动"
|
|
797
|
+
foreach ($k in @('catalogSyncedAt', 'reloadedAt', 'reloadCount')) {
|
|
798
|
+
$txt = $txt -replace ('("' + $k + '"\s*:\s*)[0-9]+'), '${1}0'
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
# 本机根路径 → 占位符(换机器后由 apply 展开)
|
|
802
|
+
$txt = ConvertTo-TokenText $txt $roots
|
|
803
|
+
$norm = $txt -replace "`r`n", "`n"
|
|
804
|
+
$dstFile = Join-Path $RepoRoot $m.Repo
|
|
805
|
+
$existing = $null
|
|
806
|
+
if (Test-Path -LiteralPath $dstFile) { $existing = [IO.File]::ReadAllText($dstFile) -replace "`r`n", "`n" }
|
|
807
|
+
if ($null -ne $existing -and $existing -eq $norm) {
|
|
808
|
+
Say (" = " + $m.Repo + "(无变化)")
|
|
809
|
+
} else {
|
|
810
|
+
$dstDir = Split-Path -Parent $dstFile
|
|
811
|
+
if (-not (Test-Path -LiteralPath $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
|
|
812
|
+
[IO.File]::WriteAllText($dstFile, $norm, (New-Object Text.UTF8Encoding($false, $false)))
|
|
813
|
+
$changed.Add($m.Repo) | Out-Null
|
|
814
|
+
Ok ("更新 " + $m.Repo)
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
} else { Skip-SyncGroup $Scope $RepoRoot 'settings' }
|
|
819
|
+
|
|
820
|
+
# 6) 记忆插件数据目录 mnemon(与记忆插件配套,需要跨机一致)
|
|
821
|
+
if (Test-SyncGroupEnabled $Scope 'mnemon') {
|
|
822
|
+
$mnemonSrc = Join-Path $AppRoot 'mnemon'
|
|
823
|
+
$mnemonDst = Join-Path $RepoRoot 'mnemon'
|
|
824
|
+
if (Test-Path -LiteralPath $mnemonSrc) {
|
|
825
|
+
$mnFiles = Get-ChildItem -LiteralPath $mnemonSrc -Recurse -File -Force
|
|
826
|
+
foreach ($f in $mnFiles) {
|
|
827
|
+
$rel = Get-RelPath $mnemonSrc $f.FullName
|
|
828
|
+
if (-not $rel) { Warn "跳过(无法求相对路径):$($f.FullName)"; continue }
|
|
829
|
+
$dstFile = Join-Path $mnemonDst $rel
|
|
830
|
+
$dstDir = Split-Path -Parent $dstFile
|
|
831
|
+
if (-not (Test-Path -LiteralPath $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
|
|
832
|
+
CopyTracked $f.FullName $dstFile ("mnemon\" + $rel)
|
|
833
|
+
}
|
|
834
|
+
# 镜像:仓库里本机已不存在的记忆文件要删掉,否则两台机器会对不上
|
|
835
|
+
if (Test-Path -LiteralPath $mnemonDst) {
|
|
836
|
+
Get-ChildItem -LiteralPath $mnemonDst -Recurse -File -Force | ForEach-Object {
|
|
837
|
+
$rel = Get-RelPath $mnemonDst $_.FullName
|
|
838
|
+
if ($rel -and -not (Test-Path -LiteralPath (Join-Path $mnemonSrc $rel))) {
|
|
839
|
+
Remove-Item -LiteralPath $_.FullName -Force
|
|
840
|
+
$changed.Add("mnemon\$rel(删除)") | Out-Null
|
|
841
|
+
Warn "仓库中移除本机已不存在的记忆文件:$rel"
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
Ok "已抓取记忆数据(mnemon):$($mnFiles.Count) 个文件"
|
|
846
|
+
} else {
|
|
847
|
+
Warn "本机没有 $mnemonSrc(记忆插件的 -AppRoot 可能不同),跳过记忆数据"
|
|
848
|
+
}
|
|
849
|
+
} else { Skip-SyncGroup $Scope $RepoRoot 'mnemon' }
|
|
850
|
+
|
|
851
|
+
# 6) 本地源码插件打包
|
|
852
|
+
if (Test-SyncGroupEnabled $Scope 'local-plugins') {
|
|
853
|
+
if ($PackLocalPlugins) {
|
|
854
|
+
$pkg = Get-Content -LiteralPath (Join-Path $ProfileDir 'package.json') -Raw -Encoding utf8 | ConvertFrom-Json
|
|
855
|
+
foreach ($prop in $pkg.dependencies.PSObject.Properties) {
|
|
856
|
+
if ($prop.Value -notmatch '^link:') { continue }
|
|
857
|
+
$linkPath = $prop.Value.Substring(5) -replace '/', '\'
|
|
858
|
+
if (-not (Test-Path -LiteralPath $linkPath)) { Warn "link 目标不存在,跳过:$linkPath"; continue }
|
|
859
|
+
$dirName = Split-Path -Leaf $linkPath
|
|
860
|
+
if (-not (Test-SyncItemEnabled $Scope 'local-plugins' $dirName)) {
|
|
861
|
+
Skip-SyncItem $Scope $RepoRoot 'local-plugins' $dirName ("local-plugins\" + $dirName + ".zip")
|
|
862
|
+
continue
|
|
863
|
+
}
|
|
864
|
+
$zipPath = Join-Path $RepoRoot "local-plugins\$dirName.zip"
|
|
865
|
+
$tgzPath = Join-Path $RepoRoot "local-plugins\$dirName.tgz"
|
|
866
|
+
if (Test-Path -LiteralPath $tgzPath) { Remove-Item -LiteralPath $tgzPath -Force }
|
|
867
|
+
if (Test-Path -LiteralPath $zipPath) { Remove-Item -LiteralPath $zipPath -Force }
|
|
868
|
+
Compress-Archive -LiteralPath $linkPath -DestinationPath $zipPath -CompressionLevel Optimal -Force
|
|
869
|
+
$changed.Add("local-plugins\$dirName.zip") | Out-Null
|
|
870
|
+
$ver = '?'
|
|
871
|
+
$p = Join-Path $linkPath 'package.json'
|
|
872
|
+
if (Test-Path -LiteralPath $p) { $ver = (Get-Content -LiteralPath $p -Raw -Encoding utf8 | ConvertFrom-Json).version }
|
|
873
|
+
Ok ("已打包 " + $dirName + "(version " + $ver + ",{0:N1} MB)" -f ((Get-Item -LiteralPath $zipPath).Length / 1MB))
|
|
874
|
+
# 路径里若有版本号,和 package.json 的 version 对不上就提醒改名
|
|
875
|
+
if ($dirName -match '([0-9]+\.[0-9]+\.[0-9]+)$' -and $ver -ne $Matches[1]) {
|
|
876
|
+
Warn "目录名版本($($Matches[1])) 与插件实际版本($ver) 不一致,建议核对是否换了新版本"
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
} else { Skip-SyncGroup $Scope $RepoRoot 'local-plugins' }
|
|
882
|
+
|
|
883
|
+
# 6.5) 插件数据(0.3.51 起):<DshHome> 一级目录里各插件自己的配置/数据
|
|
884
|
+
# 规则(排除名单、单文件上限)来自 <DshHome>\config-sync-extra.json,可在面板里改。
|
|
885
|
+
# 已经被「插件设置与工作流」「记忆数据」覆盖的文件不重复收,避免仓库里出现两份。
|
|
886
|
+
function CopyTrackedTokens($src, $dst, $label, $roots) {
|
|
887
|
+
# 文本:占位符化({{APP_ROOT}} / {{DSH_HOME}} / {{WORKSPACE_HOME}})+ LF;二进制:字节保真
|
|
888
|
+
$ext = [IO.Path]::GetExtension($src).ToLower()
|
|
889
|
+
$bin = $ext -in @('.tgz', '.zip', '.png', '.jpg', '.jpeg', '.gif', '.ico', '.bmp', '.webp', '.exe', '.dll', '.wasm', '.pdf', '.dat', '.woff', '.woff2', '.ttf', '.otf')
|
|
890
|
+
if (-not $bin) {
|
|
891
|
+
$probe = [IO.File]::ReadAllBytes($src)
|
|
892
|
+
if ($probe.Length -gt 0 -and $probe -contains 0) { $bin = $true }
|
|
893
|
+
}
|
|
894
|
+
if ($bin) { CopyTracked $src $dst $label; return }
|
|
895
|
+
$bytes = [IO.File]::ReadAllBytes($src)
|
|
896
|
+
$hasBom = ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF)
|
|
897
|
+
$text = [Text.Encoding]::UTF8.GetString($bytes)
|
|
898
|
+
if ($hasBom -and $text.Length -gt 0 -and $text[0] -eq [char]0xFEFF) { $text = $text.Substring(1) }
|
|
899
|
+
$text = $text -replace "`r`n", "`n"
|
|
900
|
+
$text = ConvertTo-TokenText $text $roots
|
|
901
|
+
if (Test-Path -LiteralPath $dst) {
|
|
902
|
+
if ((Get-RepoText $dst) -ceq $text) { Say (" = " + $label + "(无变化)"); return }
|
|
903
|
+
}
|
|
904
|
+
[IO.File]::WriteAllText($dst, $text, (New-Object Text.UTF8Encoding($hasBom, $false)))
|
|
905
|
+
$changed.Add($label) | Out-Null
|
|
906
|
+
Ok ("更新 " + $label)
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
$ExtraCfg = Get-SyncExtraConfig $DshHome
|
|
910
|
+
|
|
911
|
+
if (-not (Test-SyncGroupEnabled $Scope 'plugin-data') -or $ExtraCfg.pluginData.enabled -eq $false) {
|
|
912
|
+
if (Test-SyncGroupEnabled $Scope 'plugin-data') { Warn '插件数据:已在 config-sync-extra.json 里关闭(pluginData.enabled=false),跳过' }
|
|
913
|
+
else { Skip-SyncGroup $Scope $RepoRoot 'plugin-data' }
|
|
914
|
+
} else {
|
|
915
|
+
$pdRoots = Get-SyncRoots $DshHome $AppRoot $WorkspaceHome
|
|
916
|
+
$pdDst = Join-Path $RepoRoot 'plugin-data'
|
|
917
|
+
if (-not (Test-Path -LiteralPath $pdDst)) { New-Item -ItemType Directory -Path $pdDst -Force | Out-Null }
|
|
918
|
+
Reset-SyncExtraSecretSkips
|
|
919
|
+
$pdSlots = New-Object System.Collections.ArrayList
|
|
920
|
+
foreach ($slot in (Get-PluginDataSlots $DshHome $ExtraCfg)) {
|
|
921
|
+
if (-not (Test-SyncItemEnabled $Scope 'plugin-data' $slot.id)) {
|
|
922
|
+
Skip-SyncItem $Scope $RepoRoot 'plugin-data' $slot.id ('plugin-data\' + $slot.id)
|
|
923
|
+
continue
|
|
924
|
+
}
|
|
925
|
+
$slotDst = Join-Path $pdDst $slot.id
|
|
926
|
+
$files = @(Get-PluginDataFiles $slot.src $ExtraCfg)
|
|
927
|
+
$bytes = 0
|
|
928
|
+
foreach ($f in $files) { $bytes += $f.size }
|
|
929
|
+
if ($files.Count -eq 0) {
|
|
930
|
+
# 本机这个插件目录里没有符合规则的文件(全是大文件/缓存/密钥),仓库里就别留空壳
|
|
931
|
+
if (Test-Path -LiteralPath $slotDst) {
|
|
932
|
+
Remove-Item -LiteralPath $slotDst -Recurse -Force
|
|
933
|
+
$changed.Add('plugin-data\' + $slot.id + '(删除)') | Out-Null
|
|
934
|
+
}
|
|
935
|
+
Say (' - ' + $slot.id + '(无可同步文件,跳过)')
|
|
936
|
+
continue
|
|
937
|
+
}
|
|
938
|
+
foreach ($f in $files) {
|
|
939
|
+
$dstFile = Join-Path $slotDst $f.rel
|
|
940
|
+
$dstDir = Split-Path -Parent $dstFile
|
|
941
|
+
if (-not (Test-Path -LiteralPath $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
|
|
942
|
+
CopyTrackedTokens $f.full $dstFile ('plugin-data\' + $slot.id + '\' + $f.rel) $pdRoots
|
|
943
|
+
}
|
|
944
|
+
# 镜像:本机已不存在的文件从仓库删掉(排除名单改过、插件删了配置都会走到这里)
|
|
945
|
+
if (Test-Path -LiteralPath $slotDst) {
|
|
946
|
+
Get-ChildItem -LiteralPath $slotDst -Recurse -File -Force | ForEach-Object {
|
|
947
|
+
$rel = Get-RelPath $slotDst $_.FullName
|
|
948
|
+
if ($rel -and -not (Test-Path -LiteralPath (Join-Path $slot.src $rel))) {
|
|
949
|
+
Remove-Item -LiteralPath $_.FullName -Force
|
|
950
|
+
$changed.Add('plugin-data\' + $slot.id + '\' + $rel + '(删除)') | Out-Null
|
|
951
|
+
Warn ('仓库中移除本机已不存在的文件:' + $slot.id + '\' + $rel)
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
[void]$pdSlots.Add([ordered]@{
|
|
956
|
+
id = $slot.id
|
|
957
|
+
src = (ConvertTo-TokenText $slot.src $pdRoots)
|
|
958
|
+
files = $files.Count
|
|
959
|
+
bytes = [int64]$bytes
|
|
960
|
+
})
|
|
961
|
+
Ok ('插件数据 ' + $slot.id + ':' + $files.Count + ' 个文件,' + ('{0:N0}' -f ($bytes / 1KB)) + ' KB')
|
|
962
|
+
}
|
|
963
|
+
# manifest 不写时间戳:写了会让每次抓取都产生"有改动",白白多一个提交
|
|
964
|
+
$pdManifest = [ordered]@{ version = 1; slots = @($pdSlots) }
|
|
965
|
+
$pdManifestPath = Join-Path $pdDst 'manifest.json'
|
|
966
|
+
$pdManifestText = ($pdManifest | ConvertTo-Json -Depth 6)
|
|
967
|
+
if (Test-Path -LiteralPath $pdManifestPath) {
|
|
968
|
+
if ((Get-RepoText $pdManifestPath) -ceq $pdManifestText) { Say ' = plugin-data\manifest.json(无变化)' }
|
|
969
|
+
else { [IO.File]::WriteAllText($pdManifestPath, $pdManifestText, (New-Object Text.UTF8Encoding($false))); $changed.Add('plugin-data\manifest.json') | Out-Null; Ok '更新 plugin-data\manifest.json' }
|
|
970
|
+
} else {
|
|
971
|
+
[IO.File]::WriteAllText($pdManifestPath, $pdManifestText, (New-Object Text.UTF8Encoding($false))); $changed.Add('plugin-data\manifest.json') | Out-Null; Ok '新增 plugin-data\manifest.json'
|
|
972
|
+
}
|
|
973
|
+
Ok ('插件数据:本轮 ' + $pdSlots.Count + ' 个槽参与同步')
|
|
974
|
+
$secretSkips = @(Get-SyncExtraSecretSkips)
|
|
975
|
+
if ($secretSkips.Count -gt 0) {
|
|
976
|
+
# 名字看不出、内容里带 accessToken / apiKey / password 的文件:不进仓库,只提示
|
|
977
|
+
$head = ($secretSkips | Select-Object -First 6) -join '、'
|
|
978
|
+
Warn ('插件数据:' + $secretSkips.Count + ' 个文件内容含凭据(accessToken / apiKey / password 等),已跳过不进仓库:' + $head + $(if ($secretSkips.Count -gt 6) { ' …' } else { '' }))
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
# 6.6) 额外路径(0.3.51 起):用户在面板里自己填的文件/目录
|
|
983
|
+
if (-not (Test-SyncGroupEnabled $Scope 'extra') -or $ExtraCfg.extra.enabled -eq $false) {
|
|
984
|
+
if (Test-SyncGroupEnabled $Scope 'extra') { Warn '额外路径:已在 config-sync-extra.json 里关闭(extra.enabled=false),跳过' }
|
|
985
|
+
else { Skip-SyncGroup $Scope $RepoRoot 'extra' }
|
|
986
|
+
} elseif (@($ExtraCfg.extra.paths).Count -eq 0) {
|
|
987
|
+
Say ' - 额外路径:没有登记任何路径(面板「额外路径」卡里可以加)'
|
|
988
|
+
} else {
|
|
989
|
+
$exRoots = Get-SyncRoots $DshHome $AppRoot $WorkspaceHome
|
|
990
|
+
$exDst = Join-Path $RepoRoot 'extra'
|
|
991
|
+
if (-not (Test-Path -LiteralPath $exDst)) { New-Item -ItemType Directory -Path $exDst -Force | Out-Null }
|
|
992
|
+
$exItems = New-Object System.Collections.ArrayList
|
|
993
|
+
foreach ($p in @($ExtraCfg.extra.paths)) {
|
|
994
|
+
$srcPath = [string]$p.src
|
|
995
|
+
if (-not (Test-Path -LiteralPath $srcPath)) { Warn ('额外路径本机不存在,跳过:' + $srcPath); continue }
|
|
996
|
+
$srcTok = ConvertTo-TokenText $srcPath $exRoots
|
|
997
|
+
if (-not $srcTok.Contains('{{')) {
|
|
998
|
+
# 不在 .dsh / 客户端目录 / 工作区根之下:换台机器 apply 会按同一个绝对路径铺,路径不存在就只能跳过
|
|
999
|
+
Warn ('额外路径不在已知根下(换机可能对不上):' + $srcPath)
|
|
1000
|
+
}
|
|
1001
|
+
if (-not (Test-SyncItemEnabled $Scope 'extra' $p.id)) { Skip-SyncItem $Scope $RepoRoot 'extra' $p.id ('extra\' + $p.id); continue }
|
|
1002
|
+
$isDir = (Get-Item -LiteralPath $srcPath -Force).PSIsContainer
|
|
1003
|
+
$itemDst = Join-Path $exDst $p.id
|
|
1004
|
+
$nFiles = 0; $nBytes = 0
|
|
1005
|
+
if ($isDir) {
|
|
1006
|
+
if ((Test-Path -LiteralPath $itemDst) -and -not (Get-Item -LiteralPath $itemDst -Force).PSIsContainer) { Remove-Item -LiteralPath $itemDst -Force }
|
|
1007
|
+
if (-not (Test-Path -LiteralPath $itemDst)) { New-Item -ItemType Directory -Path $itemDst -Force | Out-Null }
|
|
1008
|
+
$files = @(Get-PluginDataFiles $srcPath $ExtraCfg)
|
|
1009
|
+
foreach ($f in $files) {
|
|
1010
|
+
$dstFile = Join-Path $itemDst $f.rel
|
|
1011
|
+
$dstDir = Split-Path -Parent $dstFile
|
|
1012
|
+
if (-not (Test-Path -LiteralPath $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
|
|
1013
|
+
CopyTrackedTokens $f.full $dstFile ('extra\' + $p.id + '\' + $f.rel) $exRoots
|
|
1014
|
+
$nFiles++; $nBytes += $f.size
|
|
1015
|
+
}
|
|
1016
|
+
if (Test-Path -LiteralPath $itemDst) {
|
|
1017
|
+
Get-ChildItem -LiteralPath $itemDst -Recurse -File -Force | ForEach-Object {
|
|
1018
|
+
$rel = Get-RelPath $itemDst $_.FullName
|
|
1019
|
+
if ($rel -and -not (Test-Path -LiteralPath (Join-Path $srcPath $rel))) {
|
|
1020
|
+
Remove-Item -LiteralPath $_.FullName -Force
|
|
1021
|
+
$changed.Add('extra\' + $p.id + '\' + $rel + '(删除)') | Out-Null
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
Ok ('额外路径 ' + $p.id + ':目录,' + $nFiles + ' 个文件')
|
|
1026
|
+
} else {
|
|
1027
|
+
$itemDst = Join-Path $exDst ($p.id + (Split-Path -Extension $srcPath))
|
|
1028
|
+
if ((Test-Path -LiteralPath $itemDst) -and (Get-Item -LiteralPath $itemDst -Force).PSIsContainer) { Remove-Item -LiteralPath $itemDst -Recurse -Force }
|
|
1029
|
+
if (Test-SyncExtraSecret $srcPath) {
|
|
1030
|
+
# 你自己加进来的路径:尊重设置照抓,但提醒一句(进仓库就等于进了远端历史)
|
|
1031
|
+
Warn ('额外路径 ' + $p.id + ':文件内容含凭据(accessToken / apiKey / password 等),建议别同步;本次仍按你的设置抓取')
|
|
1032
|
+
}
|
|
1033
|
+
CopyTrackedTokens $srcPath $itemDst ('extra\' + (Split-Path -Leaf $itemDst)) $exRoots
|
|
1034
|
+
$nFiles = 1; $nBytes = (Get-Item -LiteralPath $srcPath).Length
|
|
1035
|
+
Ok ('额外路径 ' + $p.id + ':单个文件 ' + (Split-Path -Leaf $srcPath))
|
|
1036
|
+
}
|
|
1037
|
+
[void]$exItems.Add([ordered]@{
|
|
1038
|
+
id = $p.id
|
|
1039
|
+
label = $p.label
|
|
1040
|
+
src = (ConvertTo-TokenText $srcPath $exRoots)
|
|
1041
|
+
kind = $(if ($isDir) { 'dir' } else { 'file' })
|
|
1042
|
+
files = $nFiles
|
|
1043
|
+
bytes = [int64]$nBytes
|
|
1044
|
+
})
|
|
1045
|
+
}
|
|
1046
|
+
$exManifestPath = Join-Path $exDst 'manifest.json'
|
|
1047
|
+
$exManifestText = ([ordered]@{ version = 1; items = @($exItems) } | ConvertTo-Json -Depth 6)
|
|
1048
|
+
if (Test-Path -LiteralPath $exManifestPath) {
|
|
1049
|
+
if ((Get-RepoText $exManifestPath) -cne $exManifestText) {
|
|
1050
|
+
[IO.File]::WriteAllText($exManifestPath, $exManifestText, (New-Object Text.UTF8Encoding($false)))
|
|
1051
|
+
$changed.Add('extra\manifest.json') | Out-Null; Ok '更新 extra\manifest.json'
|
|
1052
|
+
} else { Say ' = extra\manifest.json(无变化)' }
|
|
1053
|
+
} else {
|
|
1054
|
+
[IO.File]::WriteAllText($exManifestPath, $exManifestText, (New-Object Text.UTF8Encoding($false)))
|
|
1055
|
+
$changed.Add('extra\manifest.json') | Out-Null; Ok '新增 extra\manifest.json'
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
# 7) 工作区 + 会话同步(默认全量;放在最后,前面的配置都抓完再抓资料)
|
|
1061
|
+
if (-not $SkipWorkspace) {
|
|
1062
|
+
if (Test-SyncGroupEnabled $Scope 'workspace') { Sync-WorkspaceCapture }
|
|
1063
|
+
else { Skip-SyncGroup $Scope $RepoRoot 'workspace' }
|
|
1064
|
+
if (Test-SyncGroupEnabled $Scope 'conversations') { Sync-ConversationCapture }
|
|
1065
|
+
else { Skip-SyncGroup $Scope $RepoRoot 'conversations' }
|
|
1066
|
+
} else {
|
|
1067
|
+
Warn "已指定 -SkipWorkspace,跳过全部工作区与会话同步"
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
Say ""
|
|
1071
|
+
if ($changed.Count -eq 0) {
|
|
1072
|
+
Ok "本机配置与仓库一致,无需提交"
|
|
1073
|
+
} else {
|
|
1074
|
+
Say "本次有 $($changed.Count) 处变更:"
|
|
1075
|
+
$changed | ForEach-Object { Say " - $_" }
|
|
1076
|
+
Say ""
|
|
1077
|
+
Say "接着执行(二选一):"
|
|
1078
|
+
Say " powershell -File .\sync.ps1 push # 抓取 + 提交 + 推送,一条命令"
|
|
1079
|
+
Say " 或手动:git add -A ; git commit -m `"sync from <机器名>`" ; git push"
|
|
1080
|
+
Say "另一台机器:powershell -File .\sync.ps1 pull"
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
# 7) 根外绝对路径体检(0.3.36 起)
|
|
1084
|
+
# 仓库里的配置本该只剩占位符;如果还留着「不属于任何已确认根」的绝对路径,
|
|
1085
|
+
# 换台机器 pull 下来就是一条死路径(比如 F:\workspace)。这里扫一遍记成清单,
|
|
1086
|
+
# 面板「本机路径」卡会把它列出来 —— 不拦提交,只让人看见。
|
|
1087
|
+
function Write-OutsideRootReport {
|
|
1088
|
+
$rr = Get-SyncRoots $DshHome $AppRoot $WorkspaceHome
|
|
1089
|
+
$norm = New-Object System.Collections.ArrayList
|
|
1090
|
+
foreach ($v in @($rr.appRoot, $rr.dshHome, $rr.workspaceHome)) {
|
|
1091
|
+
if ($v) { [void]$norm.Add((([string]$v).Replace('\', '/').TrimEnd('/')).ToLowerInvariant()) }
|
|
1092
|
+
}
|
|
1093
|
+
if ($rr.custom) {
|
|
1094
|
+
if ($rr.custom -is [System.Collections.IDictionary]) {
|
|
1095
|
+
foreach ($k in @($rr.custom.Keys)) {
|
|
1096
|
+
$v = [string]$rr.custom[$k]
|
|
1097
|
+
if ($v) { [void]$norm.Add(($v.Replace('\', '/').TrimEnd('/')).ToLowerInvariant()) }
|
|
1098
|
+
}
|
|
1099
|
+
} else {
|
|
1100
|
+
foreach ($p in @($rr.custom.PSObject.Properties)) {
|
|
1101
|
+
$v = [string]$p.Value
|
|
1102
|
+
if ($v) { [void]$norm.Add(($v.Replace('\', '/').TrimEnd('/')).ToLowerInvariant()) }
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
$seen = @{}
|
|
1107
|
+
$items = New-Object System.Collections.ArrayList
|
|
1108
|
+
# 分两类统计:
|
|
1109
|
+
# config —— 真正要跟着机器走的配置(settings/profile/skills/presets/prompt/顶层 json)
|
|
1110
|
+
# data —— 工作区与会话的登记/缓存(workspace-registry、session-index、session_projcache)
|
|
1111
|
+
# 这些路径换机器后必须被改成本机对应目录,否则工作区打不开、历史会话回不到正确目录,
|
|
1112
|
+
# 所以它们同样要报,只是归到「数据」一组,别和配置混在一起。
|
|
1113
|
+
# 会话日志(zstd)和附件(blob)是二进制,不参与文本匹配。
|
|
1114
|
+
$confTop = @('settings', 'profile', 'skills', 'presets', 'prompt', 'local-plugins', 'plugin')
|
|
1115
|
+
$dataTop = @('workspace', 'conversations')
|
|
1116
|
+
$files = @(Get-ChildItem -LiteralPath $RepoRoot -Recurse -File -Force -ErrorAction SilentlyContinue |
|
|
1117
|
+
Where-Object {
|
|
1118
|
+
$rel0 = $_.FullName.Substring($RepoRoot.Length).TrimStart('\', '/').Replace('\', '/')
|
|
1119
|
+
(($confTop -contains ($rel0 -split '/')[0]) -or ($dataTop -contains ($rel0 -split '/')[0]) -or ($rel0 -notmatch '/')) -and
|
|
1120
|
+
$rel0 -notmatch 'node_modules' -and
|
|
1121
|
+
$_.Length -lt 4194304 -and
|
|
1122
|
+
($_.Extension -eq '.json' -or $_.Extension -eq '.jsonc' -or $_.Extension -eq '.yml' -or $_.Extension -eq '.yaml')
|
|
1123
|
+
})
|
|
1124
|
+
$nConf = 0
|
|
1125
|
+
$nData = 0
|
|
1126
|
+
foreach ($f in $files) {
|
|
1127
|
+
$txt = ''
|
|
1128
|
+
try { $txt = [IO.File]::ReadAllText($f.FullName) } catch { continue }
|
|
1129
|
+
$rel = $f.FullName.Substring($RepoRoot.Length).TrimStart('\', '/').Replace('\', '/')
|
|
1130
|
+
$scope = if ($dataTop -contains ($rel -split '/')[0]) { 'data' } else { 'config' }
|
|
1131
|
+
foreach ($m in [regex]::Matches($txt, '[A-Za-z]:[\\/][^"\s,;:}{\[\]]+')) {
|
|
1132
|
+
$raw = $m.Value
|
|
1133
|
+
$p = $raw.Replace('\', '/')
|
|
1134
|
+
if ($p -match '://') { continue } # s://1v3.apiipu.com 这种是正则把协议头当成了盘符
|
|
1135
|
+
$pn = $p.ToLowerInvariant()
|
|
1136
|
+
$inside = $false
|
|
1137
|
+
foreach ($r in $norm) { if ($r -and ($pn -eq $r -or $pn.StartsWith($r + '/') -or $pn.StartsWith($r + '\'))) { $inside = $true; break } }
|
|
1138
|
+
if ($inside) { continue }
|
|
1139
|
+
$key = $rel + '|' + $pn
|
|
1140
|
+
if ($seen.ContainsKey($key)) { continue }
|
|
1141
|
+
if ($scope -eq 'config' -and $nConf -ge 50) { continue }
|
|
1142
|
+
if ($scope -eq 'data' -and $nData -ge 30) { continue }
|
|
1143
|
+
$seen[$key] = $true
|
|
1144
|
+
[void]$items.Add([pscustomobject][ordered]@{ file = $rel; path = $p; scope = $scope })
|
|
1145
|
+
if ($scope -eq 'config') { $nConf++ } else { $nData++ }
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
$reportPath = Join-Path $DshHome 'config-sync-absolute-paths.json'
|
|
1149
|
+
if ($items.Count -eq 0) {
|
|
1150
|
+
if (Test-Path -LiteralPath $reportPath) { try { Remove-Item -LiteralPath $reportPath -Force } catch { } }
|
|
1151
|
+
return
|
|
1152
|
+
}
|
|
1153
|
+
# 记下「这份配置是哪台机器写的、写它时各根是什么值」:面板靠它判断这份清单该不该在本机显示,
|
|
1154
|
+
# 也靠对方的根值把远端的绝对路径映射成本机路径(推荐地址就是这么算出来的)。
|
|
1155
|
+
$machine = ''
|
|
1156
|
+
try { $machine = [string]$env:COMPUTERNAME } catch { }
|
|
1157
|
+
$customOut = [ordered]@{}
|
|
1158
|
+
if ($rr.custom) {
|
|
1159
|
+
if ($rr.custom -is [System.Collections.IDictionary]) {
|
|
1160
|
+
foreach ($k in @($rr.custom.Keys)) { $customOut[[string]$k] = [string]$rr.custom[$k] }
|
|
1161
|
+
} else {
|
|
1162
|
+
foreach ($pp in @($rr.custom.PSObject.Properties)) { $customOut[[string]$pp.Name] = [string]$pp.Value }
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
$obj = [ordered]@{
|
|
1166
|
+
at = (Get-Date -Format 'yyyy-MM-dd HH:mm:ss')
|
|
1167
|
+
machine = $machine
|
|
1168
|
+
roots = [ordered]@{
|
|
1169
|
+
appRoot = [string]$rr.appRoot
|
|
1170
|
+
dshHome = [string]$rr.dshHome
|
|
1171
|
+
workspaceHome = [string]$rr.workspaceHome
|
|
1172
|
+
custom = $customOut
|
|
1173
|
+
}
|
|
1174
|
+
total = $items.Count
|
|
1175
|
+
totalConfig = $nConf
|
|
1176
|
+
totalData = $nData
|
|
1177
|
+
items = @($items)
|
|
1178
|
+
}
|
|
1179
|
+
[IO.File]::WriteAllText($reportPath, ($obj | ConvertTo-Json -Depth 5), (New-Object Text.UTF8Encoding($false)))
|
|
1180
|
+
Warn ("有 " + $items.Count + " 处绝对路径不在任何已确认根下(配置 " + $nConf + " 处、工作区与会话 " + $nData + " 处):换机器拉取时面板会逐条给出推荐目录。")
|
|
1181
|
+
Warn (" 清单:" + $reportPath + "(面板「本机路径」卡会列出来)")
|
|
1182
|
+
foreach ($it in @($items | Select-Object -First 5)) { Warn (" - " + $it.file + " → " + $it.path) }
|
|
1183
|
+
if ($items.Count -gt 5) { Warn (" …还有 " + ($items.Count - 5) + " 处") }
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
Write-OutsideRootReport
|
|
1187
|
+
|
|
1188
|
+
exit 0
|