dsh-config-manager 0.11.0 → 0.12.0
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/lib/adapters/attachment-index-cache.d.ts +33 -0
- package/lib/adapters/attachment-index-cache.js +114 -0
- package/lib/adapters/attachments.d.ts +97 -0
- package/lib/adapters/attachments.js +448 -0
- package/lib/adapters/index.d.ts +1 -0
- package/lib/adapters/index.js +7 -1
- package/lib/adapters/sessions.d.ts +18 -1
- package/lib/adapters/sessions.js +193 -0
- package/lib/adapters/test-helpers.d.ts +7 -0
- package/lib/adapters/test-helpers.js +8 -1
- package/lib/adapters/workspaces.d.ts +0 -1
- package/lib/adapters/workspaces.js +244 -5
- package/lib/client.d.ts +119 -1
- package/lib/client.js +1537 -828
- package/lib/core/analyzer.d.ts +1 -14
- package/lib/core/analyzer.js +25 -67
- package/lib/core/backup-plan.js +4 -0
- package/lib/core/backup.js +3 -0
- package/lib/core/host-service.d.ts +12 -0
- package/lib/core/host-service.js +12 -0
- package/lib/core/host-session-shapes.d.ts +209 -0
- package/lib/core/host-session-shapes.js +134 -0
- package/lib/core/host-workspace-facade.d.ts +57 -0
- package/lib/core/host-workspace-facade.js +127 -0
- package/lib/core/import-planning.d.ts +15 -0
- package/lib/core/import-planning.js +44 -0
- package/lib/core/messages.d.ts +20 -0
- package/lib/core/messages.js +40 -0
- package/lib/core/session-analyze.d.ts +52 -0
- package/lib/core/session-analyze.js +135 -0
- package/lib/core/session-compare.d.ts +54 -0
- package/lib/core/session-compare.js +154 -0
- package/lib/core/session-meta.d.ts +32 -0
- package/lib/core/session-meta.js +49 -0
- package/lib/core/session-verdict-plan.d.ts +2 -0
- package/lib/core/session-verdict-plan.js +33 -0
- package/lib/core/types.d.ts +153 -0
- package/lib/core/unit-enrichment.d.ts +19 -0
- package/lib/core/unit-enrichment.js +39 -0
- package/lib/core/workspace-root.d.ts +46 -0
- package/lib/core/workspace-root.js +121 -0
- package/lib/index.d.ts +2 -112
- package/lib/index.js +15 -222
- package/lib/schema/section-registry.js +15 -0
- package/lib/schema/types.d.ts +17 -1
- package/lib/ui/export-flow.js +1 -0
- package/lib/ui/i18n.d.ts +1 -0
- package/lib/ui/i18n.js +2 -0
- package/lib/ui/import-wizard.d.ts +8 -0
- package/lib/ui/import-wizard.js +10 -0
- package/lib/ui/selection-model.d.ts +60 -0
- package/lib/ui/selection-model.js +258 -2
- package/lib/ui/session-compare-facts.d.ts +51 -0
- package/lib/ui/session-compare-facts.js +82 -0
- package/lib/ui/test-helpers.js +2 -2
- package/lib/utils/attachment-refs.d.ts +49 -0
- package/lib/utils/attachment-refs.js +169 -0
- package/package.json +1 -1
- package/src/adapters/attachment-index-cache.test.ts +122 -0
- package/src/adapters/attachment-index-cache.ts +129 -0
- package/src/adapters/attachments.test.ts +358 -0
- package/src/adapters/attachments.ts +472 -0
- package/src/adapters/index.ts +8 -1
- package/src/adapters/sessions.test.ts +140 -0
- package/src/adapters/sessions.ts +184 -1
- package/src/adapters/test-helpers.ts +8 -1
- package/src/adapters/workspaces.test.ts +254 -0
- package/src/adapters/workspaces.ts +234 -5
- package/src/client/common/ContentPicker.tsx +235 -15
- package/src/client/common/section-labels.test.ts +1 -1
- package/src/client/common/section-labels.ts +1 -0
- package/src/client/common/ui.tsx +17 -1
- package/src/client/config-manager.module.css +29 -0
- package/src/client/export/ExportView.tsx +15 -2
- package/src/client/import/ImportWizardView.tsx +8 -0
- package/src/client/import/PathMappingForm.tsx +49 -10
- package/src/client/import/import-wizard-steps.tsx +80 -3
- package/src/client/import/use-import-wizard-controller.ts +3 -0
- package/src/client/locales/common.ts +2 -0
- package/src/client/locales/export.ts +5 -0
- package/src/client/locales/import.ts +6 -0
- package/src/client/locales/sessions.ts +26 -0
- package/src/client/run-store-redact.ts +122 -0
- package/src/client/run-store.ts +13 -115
- package/src/core/analyzer.ts +38 -79
- package/src/core/backup-plan.ts +4 -0
- package/src/core/backup.ts +3 -0
- package/src/core/host-service.ts +17 -0
- package/src/core/host-session-shapes.ts +270 -0
- package/src/core/host-workspace-facade.ts +153 -0
- package/src/core/import-planning.ts +48 -0
- package/src/core/messages.ts +40 -0
- package/src/core/native-path-picker.test.ts +3 -2
- package/src/core/session-analyze.ts +159 -0
- package/src/core/session-compare-wiring.test.ts +207 -0
- package/src/core/session-compare.test.ts +119 -0
- package/src/core/session-compare.ts +185 -0
- package/src/core/session-meta.test.ts +25 -2
- package/src/core/session-meta.ts +54 -0
- package/src/core/session-verdict-plan.test.ts +70 -0
- package/src/core/session-verdict-plan.ts +32 -0
- package/src/core/types.ts +161 -0
- package/src/core/unit-enrichment.ts +46 -0
- package/src/core/workspace-root.test.ts +152 -0
- package/src/core/workspace-root.ts +146 -0
- package/src/foreign/trae-sqlcipher.test.ts +13 -4
- package/src/index.ts +25 -355
- package/src/schema/manifest-schema.test.ts +1 -1
- package/src/schema/registry.test.ts +7 -2
- package/src/schema/section-registry.ts +15 -0
- package/src/schema/types.ts +23 -2
- package/src/sync/transport.test.ts +1 -1
- package/src/ui/export-flow.ts +1 -0
- package/src/ui/i18n.ts +2 -0
- package/src/ui/import-wizard.ts +11 -0
- package/src/ui/selection-model.test.ts +265 -3
- package/src/ui/selection-model.ts +256 -2
- package/src/ui/session-compare-facts.test.ts +67 -0
- package/src/ui/session-compare-facts.ts +122 -0
- package/src/ui/test-helpers.ts +2 -2
- package/src/utils/attachment-refs.test.ts +98 -0
- package/src/utils/attachment-refs.ts +177 -0
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* attachments 分区 adapter(P1,默认关):只带走**被对话真正引用过**的附件。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不整目录搬:本机实测 `$DSH_HOME/attachments` 233 MB / 1374 个对象,其中 68 MB 是
|
|
5
|
+
* 没有任何会话引用的**孤儿**(删除会话后留下的对象)。整库备份会把孤儿一起搬去新机,
|
|
6
|
+
* 用户付出体积与拷贝时间却得不到任何可看的东西。
|
|
7
|
+
*
|
|
8
|
+
* 判据来源:会话日志里的 `"attachmentId":"sha256:<64hex>"`(见 `utils/attachment-refs.ts`)。
|
|
9
|
+
* 只看**日志文本**,不看文件时间、不看最近使用 —— 「用过」是唯一可复现的口径。
|
|
10
|
+
*
|
|
11
|
+
* 收窄范围:`options.includeItems.sessions`(与 sessions adapter 同一份单元 id
|
|
12
|
+
* `<projectKey>/<sessionId>`):
|
|
13
|
+
* - 空数组(用户把会话分区清空)⇒ 本分区一条都不带并告警(与基类 allow 语义一致);
|
|
14
|
+
* - 键缺省(没指定会话范围)⇒ 按**全部会话**算引用面,并如实告警,
|
|
15
|
+
* 既不能静默当成「只用了选中会话」,也不能当成「没有附件」。
|
|
16
|
+
* `export.sessions.limit`(最新 N 个)**不重复实现**:那是按 mtime 排序的选择,本分区
|
|
17
|
+
* 不复刻排序规则,而是如实告警「按全部会话收集,未按最新 N 个收窄」——宁可多带。
|
|
18
|
+
*
|
|
19
|
+
* 失败方向:会话日志读不出来 / zstd 解不开 ⇒ **退回整库 + 明确告警**(keep=null)。
|
|
20
|
+
* 宁可多带 233 MB,也不静默交出一个「备份成功但图片全丢」的包。
|
|
21
|
+
*/
|
|
22
|
+
import { FileCollectionAdapter } from './file-collection.js';
|
|
23
|
+
import { listFilesDetailed } from './link-report.js';
|
|
24
|
+
import { msgOf } from '../core/messages.js';
|
|
25
|
+
import './messages.js';
|
|
26
|
+
import { isSessionLogFileName, normalizeSessionLimit, sessionIdKey } from '../core/session-select.js';
|
|
27
|
+
import { attachmentIdOfRelPath, extractAttachmentRefs } from '../utils/attachment-refs.js';
|
|
28
|
+
import { decodeZstdFrame, scanZstdFrames, zstdAvailable } from '../utils/zstd-frame.js';
|
|
29
|
+
import { loadAttachmentRefCache, saveAttachmentRefCache, } from './attachment-index-cache.js';
|
|
30
|
+
import { toPosixRel } from './units.js';
|
|
31
|
+
/** 逐批并发读日志的批大小:会话库可能有上千个日志,一次性开满会打爆 libuv 线程池。 */
|
|
32
|
+
const READ_BATCH = 8;
|
|
33
|
+
/**
|
|
34
|
+
* 附件单元的短展示名:`sha256:abcd1234…`。
|
|
35
|
+
*
|
|
36
|
+
* 64 位 hex 对用户没有信息量,但完全省略又会让「这张图和那张图」无法区分,
|
|
37
|
+
* 所以只保留前 8 位 —— 足够在同一会话内分辨,又不至于把哈希塞满整行。
|
|
38
|
+
*/
|
|
39
|
+
function shortAttachmentId(ref) {
|
|
40
|
+
const hex = ref.startsWith('sha256:') ? ref.slice('sha256:'.length) : ref;
|
|
41
|
+
return hex.length <= 8 ? ref : `sha256:${hex.slice(0, 8)}…`;
|
|
42
|
+
}
|
|
43
|
+
/** 前 4 字节是否为 zstd 帧魔数(小端 0x28 0xb5 0x2f 0xfd)。 */
|
|
44
|
+
function hasZstdMagic(bytes) {
|
|
45
|
+
if (bytes.length < 4)
|
|
46
|
+
return false;
|
|
47
|
+
return bytes[0] === 0x28 && bytes[1] === 0xb5 && bytes[2] === 0x2f && bytes[3] === 0xfd;
|
|
48
|
+
}
|
|
49
|
+
const SCAN_CACHE_TTL_MS = 60_000;
|
|
50
|
+
let scanCache = null;
|
|
51
|
+
/**
|
|
52
|
+
* 归属扫描的日志条数上限(预览富化用)。
|
|
53
|
+
*
|
|
54
|
+
* 富化必须**有界**:它挂在只读预览端点上,超出上限就放弃归属(界面退回扁平渲染),
|
|
55
|
+
* 绝不让一次预览因为几千个会话日志卡住。真实导出不受这条限制 —— 那里按用户勾选收窄。
|
|
56
|
+
*/
|
|
57
|
+
const ENRICH_MAX_LOGS = 2000;
|
|
58
|
+
async function scanKeyOf(ctx, logs) {
|
|
59
|
+
const mtime = ctx.fs.mtimeMs;
|
|
60
|
+
if (mtime === undefined)
|
|
61
|
+
return null;
|
|
62
|
+
const parts = [];
|
|
63
|
+
for (let i = 0; i < logs.length; i += READ_BATCH) {
|
|
64
|
+
const batch = logs.slice(i, i + READ_BATCH);
|
|
65
|
+
const times = await Promise.all(batch.map(async (rel) => {
|
|
66
|
+
const posix = toPosixRel(rel);
|
|
67
|
+
try {
|
|
68
|
+
return `${posix}:${String(await mtime.call(ctx.fs, posix))}`;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}));
|
|
74
|
+
for (const part of times) {
|
|
75
|
+
if (part === null)
|
|
76
|
+
return null;
|
|
77
|
+
parts.push(part);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return parts.join('|');
|
|
81
|
+
}
|
|
82
|
+
/** 测试用:清空引用面缓存。生产失效只靠 mtime 键与 TTL,不靠这里。 */
|
|
83
|
+
export function resetAttachmentsScanCache() {
|
|
84
|
+
scanCache = null;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 日志字节 → 该文件引用的附件(损坏 / 解不开返回 null,由调用方降级)。
|
|
88
|
+
*
|
|
89
|
+
* 纯函数是**有意的**:导出侧从本机盘读日志、导入侧从备份包里的同一个文件字节解析 ——
|
|
90
|
+
* 两条路径共用这一份解析口径。写两份,迟早出现「导出时算得出归属、导入时算不出」,
|
|
91
|
+
* 界面与包内容又会分叉。
|
|
92
|
+
*/
|
|
93
|
+
export function refsOfLogBytes(bytes) {
|
|
94
|
+
const out = new Map();
|
|
95
|
+
let scan;
|
|
96
|
+
try {
|
|
97
|
+
scan = scanZstdFrames(bytes);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return null; // 帧头损坏:该文件判为不可读,交由调用方降级(绝不当成「零引用」)
|
|
101
|
+
}
|
|
102
|
+
const frames = scan.frames;
|
|
103
|
+
if (frames.length === 0) {
|
|
104
|
+
// 有 zstd 魔数却一帧都切不出来 = 日志损坏。**不能**当成「零引用」:
|
|
105
|
+
// 那会让一次坏日志静默缩小备份范围(用户看到「成功」而图片没进包)。
|
|
106
|
+
if (hasZstdMagic(bytes))
|
|
107
|
+
return null;
|
|
108
|
+
// 非 zstd 会话日志(历史明文层级):直接按文本抽,不用解压。
|
|
109
|
+
for (const [id, meta] of extractAttachmentRefs(Buffer.from(bytes).toString('utf8'))) {
|
|
110
|
+
out.set(id, { ...meta });
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
for (const frame of frames) {
|
|
115
|
+
try {
|
|
116
|
+
const text = decodeZstdFrame(bytes.subarray(frame.start, frame.end)).toString('utf8');
|
|
117
|
+
for (const [id, meta] of extractAttachmentRefs(text)) {
|
|
118
|
+
// 同一日志多帧可能重复引用同一附件:保留首个非空声明(后写的重试不该覆盖首次声明)
|
|
119
|
+
const prev = out.get(id);
|
|
120
|
+
out.set(id, {
|
|
121
|
+
...(prev?.mediaType !== undefined ? { mediaType: prev.mediaType } : meta.mediaType !== undefined ? { mediaType: meta.mediaType } : {}),
|
|
122
|
+
...(prev?.bytes !== undefined ? { bytes: prev.bytes } : meta.bytes !== undefined ? { bytes: meta.bytes } : {}),
|
|
123
|
+
...(prev?.name !== undefined ? { name: prev.name } : meta.name !== undefined ? { name: meta.name } : {}),
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
// 单帧解不开:该文件整体判为不可读(不拿半份引用当「完整」)
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
/** 日志文件 → 该文件引用的附件(读不出 / 解不开返回 null,由调用方降级)。 */
|
|
135
|
+
async function refsOfLog(ctx, homeRel) {
|
|
136
|
+
let bytes;
|
|
137
|
+
try {
|
|
138
|
+
bytes = await ctx.fs.readFile(homeRel);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
return refsOfLogBytes(bytes);
|
|
144
|
+
}
|
|
145
|
+
/** 日志的相对路径 → 会话单元裸键(`<projectKey>/<sessionDir>`,与 sessions 单元的 id 同构)。 */
|
|
146
|
+
function unitKeyOfLog(rel) {
|
|
147
|
+
const parts = toPosixRel(rel).split('/');
|
|
148
|
+
return parts.length >= 3 ? `${parts[1]}/${parts[2]}` : null;
|
|
149
|
+
}
|
|
150
|
+
/** 分区载荷窄化:分析阶段的 `ctx.sections` 是 `unknown`,只认真正的 files 分区(不猜其它形态)。 */
|
|
151
|
+
function isFilesSection(value) {
|
|
152
|
+
return typeof value === 'object' && value !== null && Array.isArray(value.files);
|
|
153
|
+
}
|
|
154
|
+
/** 把一份日志的引用并进索引(同一附件被多条会话引用时记多个 owner)。 */
|
|
155
|
+
function addRefs(index, unitKey, refs) {
|
|
156
|
+
for (const [id, meta] of refs) {
|
|
157
|
+
const prev = index.get(id);
|
|
158
|
+
if (prev === undefined) {
|
|
159
|
+
index.set(id, { units: [unitKey], ...meta });
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (!prev.units.includes(unitKey))
|
|
163
|
+
prev.units.push(unitKey);
|
|
164
|
+
if (prev.mediaType === undefined && meta.mediaType !== undefined)
|
|
165
|
+
prev.mediaType = meta.mediaType;
|
|
166
|
+
if (prev.bytes === undefined && meta.bytes !== undefined)
|
|
167
|
+
prev.bytes = meta.bytes;
|
|
168
|
+
if (prev.name === undefined && meta.name !== undefined)
|
|
169
|
+
prev.name = meta.name;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** 日志文件的 `mtime + size`(缓存失效键);读不到任一项返回 null = 该条不缓存。 */
|
|
173
|
+
async function statOfLog(ctx, posix) {
|
|
174
|
+
const info = ctx.fs.statInfo;
|
|
175
|
+
if (info !== undefined) {
|
|
176
|
+
try {
|
|
177
|
+
const got = await info.call(ctx.fs, posix);
|
|
178
|
+
return got === null ? null : { mtimeMs: got.mtimeMs, size: got.size };
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const mtimeMs = ctx.fs.mtimeMs;
|
|
185
|
+
const statSize = ctx.fs.statSize;
|
|
186
|
+
if (mtimeMs === undefined || statSize === undefined)
|
|
187
|
+
return null;
|
|
188
|
+
try {
|
|
189
|
+
const time = await mtimeMs.call(ctx.fs, posix);
|
|
190
|
+
const size = await statSize.call(ctx.fs, posix);
|
|
191
|
+
if (time === null || size === null)
|
|
192
|
+
return null;
|
|
193
|
+
return { mtimeMs: time, size };
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* 逐批读日志建索引;failed = 读不出/解不开的日志条数(>0 时调用方不许当成完整引用面)。
|
|
201
|
+
*
|
|
202
|
+
* 每条日志先看持久化缓存(`mtime + size` 都相等才采信),命中就不解压 —— 本机冷启 7 s、
|
|
203
|
+
* 命中后 0.05 s。缓存与本次结果合并后 best-effort 写回(写失败不影响正确性)。
|
|
204
|
+
*/
|
|
205
|
+
async function scanRefIndex(ctx, logs) {
|
|
206
|
+
const index = new Map();
|
|
207
|
+
const cached = await loadAttachmentRefCache(ctx);
|
|
208
|
+
const merged = new Map(cached);
|
|
209
|
+
let failed = 0;
|
|
210
|
+
for (let i = 0; i < logs.length; i += READ_BATCH) {
|
|
211
|
+
const batch = logs.slice(i, i + READ_BATCH);
|
|
212
|
+
const results = await Promise.all(batch.map(async (rel) => {
|
|
213
|
+
const posix = toPosixRel(rel);
|
|
214
|
+
const key = unitKeyOfLog(posix);
|
|
215
|
+
if (key === null)
|
|
216
|
+
return { key, refs: null, posix: null, stat: null };
|
|
217
|
+
const stat = await statOfLog(ctx, posix);
|
|
218
|
+
const prev = cached.get(posix);
|
|
219
|
+
if (stat !== null && prev !== undefined && prev.mtimeMs === stat.mtimeMs && prev.size === stat.size) {
|
|
220
|
+
return { key, refs: new Map(Object.entries(prev.refs)), posix, stat };
|
|
221
|
+
}
|
|
222
|
+
return { key, refs: await refsOfLog(ctx, posix), posix, stat };
|
|
223
|
+
}));
|
|
224
|
+
for (const result of results) {
|
|
225
|
+
if (result.refs === null || result.key === null) {
|
|
226
|
+
failed += 1;
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
addRefs(index, result.key, result.refs);
|
|
230
|
+
if (result.posix !== null && result.stat !== null) {
|
|
231
|
+
merged.set(result.posix, { ...result.stat, refs: Object.fromEntries(result.refs) });
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
await saveAttachmentRefCache(ctx, merged);
|
|
236
|
+
return { index, failed };
|
|
237
|
+
}
|
|
238
|
+
/** 取引用面索引(命中缓存就不重读)。导出与预览富化共用这一条路径,口径不会分叉。 */
|
|
239
|
+
async function loadRefIndex(ctx, logs) {
|
|
240
|
+
const msg = msgOf(ctx);
|
|
241
|
+
if (!zstdAvailable() && logs.length > 0)
|
|
242
|
+
return { ok: false, warning: msg('export.attachmentsNoZstd') };
|
|
243
|
+
const cacheKey = await scanKeyOf(ctx, logs);
|
|
244
|
+
const hit = cacheKey !== null && scanCache !== null
|
|
245
|
+
&& scanCache.key === cacheKey && Date.now() - scanCache.at < SCAN_CACHE_TTL_MS ? scanCache : null;
|
|
246
|
+
if (hit !== null)
|
|
247
|
+
return { ok: true, index: hit.index };
|
|
248
|
+
const { index, failed } = await scanRefIndex(ctx, logs);
|
|
249
|
+
if (failed > 0)
|
|
250
|
+
return { ok: false, warning: msg('export.attachmentsScanFailed', { count: String(failed) }) };
|
|
251
|
+
if (cacheKey !== null)
|
|
252
|
+
scanCache = { key: cacheKey, at: Date.now(), index };
|
|
253
|
+
return { ok: true, index };
|
|
254
|
+
}
|
|
255
|
+
export class AttachmentsAdapter extends FileCollectionAdapter {
|
|
256
|
+
id = 'attachments';
|
|
257
|
+
displayName = 'Attachments';
|
|
258
|
+
defaultIncluded = false;
|
|
259
|
+
portability = 'deviceSpecific';
|
|
260
|
+
baseDir = 'attachments';
|
|
261
|
+
/** 单元 = 一个附件对象(路径里那段 64 位 hex 反推出的 attachmentId)。 */
|
|
262
|
+
unitIdOf(relativePath) {
|
|
263
|
+
return attachmentIdOfRelPath(toPosixRel(relativePath)) ?? toPosixRel(relativePath);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* 导入侧富化(2026-11-21 用户要求「导入功能也适配」):把包内附件对象挂到**引用它的会话**
|
|
267
|
+
* 下面,并显示会话日志里记下的真实文件名。
|
|
268
|
+
*
|
|
269
|
+
* 为什么必须在这一层做:「谁引用过这个附件」只存在于会话日志里。导出侧由 `enrichUnits()`
|
|
270
|
+
* 读本机盘算归属;导入时目标盘上还没有这些会话,只能读**包内** sessions 分区的日志字节 ——
|
|
271
|
+
* 同一份 `refsOfLogBytes()` 解析口径,不复刻第二套。归属算得出来,「勾会话带附件」
|
|
272
|
+
* (`applySessionAttachmentCoupling`)才会自动生效,全选也不会把被引用的附件落下。
|
|
273
|
+
*
|
|
274
|
+
* 失败方向与导出侧一致:算不出归属就退回扁平(不填 parentUnitIds),孤儿附件照旧以分区行
|
|
275
|
+
* 可见可勾,绝不因为一次坏日志把整批附件藏起来,也不拦下导入。
|
|
276
|
+
*/
|
|
277
|
+
async analyzeImport(data, ctx) {
|
|
278
|
+
const items = await super.analyzeImport(data, ctx);
|
|
279
|
+
if (items.length === 0)
|
|
280
|
+
return items;
|
|
281
|
+
const index = this.refsFromPackage(ctx);
|
|
282
|
+
if (index === null)
|
|
283
|
+
return items;
|
|
284
|
+
const prefix = this.id + ':';
|
|
285
|
+
return items.map((item) => {
|
|
286
|
+
const ref = item.unitId !== undefined && item.unitId.startsWith(prefix)
|
|
287
|
+
? item.unitId.slice(prefix.length)
|
|
288
|
+
: null;
|
|
289
|
+
const entry = ref === null ? undefined : index.get(ref);
|
|
290
|
+
if (ref === null || entry === undefined || entry.units.length === 0)
|
|
291
|
+
return item;
|
|
292
|
+
const shortId = shortAttachmentId(ref);
|
|
293
|
+
const displayName = entry.name ?? entry.mediaType ?? shortId;
|
|
294
|
+
return {
|
|
295
|
+
...item,
|
|
296
|
+
label: displayName,
|
|
297
|
+
...(displayName !== shortId && item.detail === undefined ? { detail: shortId } : {}),
|
|
298
|
+
parentUnitIds: entry.units.map((key) => `sessions:${key}`),
|
|
299
|
+
};
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* 包内引用面:读 `ctx.sections` 里的 sessions 分区(分析阶段已在内存里,不碰盘)。
|
|
304
|
+
*
|
|
305
|
+
* 返回 null = 归属不可知,调用方退回扁平:任一引用了附件的日志解不开(半份引用当不得完整),
|
|
306
|
+
* 或日志条数超出预览上限。降级方向只能往「少嵌套」,绝不能往「少显示」。
|
|
307
|
+
*/
|
|
308
|
+
refsFromPackage(ctx) {
|
|
309
|
+
const sessions = ctx.sections.get('sessions');
|
|
310
|
+
if (!isFilesSection(sessions))
|
|
311
|
+
return null;
|
|
312
|
+
const logs = [];
|
|
313
|
+
for (const file of sessions.files) {
|
|
314
|
+
// 分区内相对路径不带 `sessions/` 前缀,补上再走 unitKeyOfLog —— 与导出侧(homeRel)同构。
|
|
315
|
+
const key = unitKeyOfLog(`sessions/${toPosixRel(file.relativePath)}`);
|
|
316
|
+
if (key !== null)
|
|
317
|
+
logs.push({ key, data: file.data });
|
|
318
|
+
}
|
|
319
|
+
if (logs.length === 0 || logs.length > ENRICH_MAX_LOGS)
|
|
320
|
+
return null;
|
|
321
|
+
const index = new Map();
|
|
322
|
+
for (const log of logs) {
|
|
323
|
+
const refs = refsOfLogBytes(log.data);
|
|
324
|
+
if (refs === null)
|
|
325
|
+
return null;
|
|
326
|
+
addRefs(index, log.key, refs);
|
|
327
|
+
}
|
|
328
|
+
return index;
|
|
329
|
+
}
|
|
330
|
+
async restrictUnits(ctx, rels, options) {
|
|
331
|
+
const msg = msgOf(ctx);
|
|
332
|
+
const present = new Set();
|
|
333
|
+
for (const rel of rels)
|
|
334
|
+
present.add(this.unitIdOf(this.relPathOf(rel)));
|
|
335
|
+
// ① 选中会话:键缺省 = 全量(如实告警);空数组 = 该分区一条都不带(与基类 allow 语义一致)。
|
|
336
|
+
const sessionAllow = options.includeItems?.['sessions'];
|
|
337
|
+
if (Array.isArray(sessionAllow) && sessionAllow.length === 0) {
|
|
338
|
+
return { keep: new Set(), warnings: [msg('export.attachmentsNoSessions')] };
|
|
339
|
+
}
|
|
340
|
+
const warnings = [];
|
|
341
|
+
// 未指定会话范围 = 按**全部会话**算引用面。这条必须连同真实数字一起明说,
|
|
342
|
+
// 所以推迟到 ④ 之后(届时 keep / present / missing 才齐)。
|
|
343
|
+
const allSessions = sessionAllow === undefined;
|
|
344
|
+
const limit = normalizeSessionLimit(options.sessions?.limit);
|
|
345
|
+
if (limit !== undefined && limit >= 0 && !Array.isArray(sessionAllow)) {
|
|
346
|
+
warnings.push(msg('export.attachmentsLimitIgnored'));
|
|
347
|
+
}
|
|
348
|
+
// includeItems 的单元 id 带分区前缀(sessions:<projectKey>/<sessionId>),先剥前缀再归一
|
|
349
|
+
const unitOf = (raw) => {
|
|
350
|
+
const stripped = raw.startsWith('sessions:') ? raw.slice('sessions:'.length) : raw;
|
|
351
|
+
return sessionIdKey(stripped);
|
|
352
|
+
};
|
|
353
|
+
const allowedUnits = Array.isArray(sessionAllow) ? new Set(sessionAllow.map(unitOf)) : null;
|
|
354
|
+
// ② 枚举会话日志(home 相对 'sessions/…'),按会话单元收窄
|
|
355
|
+
let logs;
|
|
356
|
+
try {
|
|
357
|
+
const listing = await listFilesDetailed(ctx.fs, 'sessions');
|
|
358
|
+
logs = listing.paths.filter((rel) => isSessionLogFileName(toPosixRel(rel).split('/').pop() ?? ''));
|
|
359
|
+
if (allowedUnits !== null) {
|
|
360
|
+
logs = logs.filter((rel) => {
|
|
361
|
+
const parts = toPosixRel(rel).split('/');
|
|
362
|
+
return parts.length >= 3 && allowedUnits.has(sessionIdKey(`${parts[1]}/${parts[2]}`));
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
catch {
|
|
367
|
+
return { keep: null, warnings: [msg('export.attachmentsScanUnreadable')] };
|
|
368
|
+
}
|
|
369
|
+
// ③ 读日志抽引用(不可读 / 不可解压 ⇒ 整批退回全库,绝不静默少带)
|
|
370
|
+
const scanned = await loadRefIndex(ctx, logs);
|
|
371
|
+
if (!scanned.ok)
|
|
372
|
+
return { keep: null, warnings: [scanned.warning] };
|
|
373
|
+
const referenced = new Set(scanned.index.keys());
|
|
374
|
+
// ④ 只留「被引用 且 本机确实存在」的对象
|
|
375
|
+
const keep = new Set();
|
|
376
|
+
let missing = 0;
|
|
377
|
+
for (const id of referenced) {
|
|
378
|
+
if (present.has(id))
|
|
379
|
+
keep.add(id);
|
|
380
|
+
else
|
|
381
|
+
missing += 1;
|
|
382
|
+
}
|
|
383
|
+
warnings.push(msg('export.attachmentsReferenced', {
|
|
384
|
+
count: String(keep.size),
|
|
385
|
+
total: String(present.size),
|
|
386
|
+
missing: String(missing),
|
|
387
|
+
}));
|
|
388
|
+
if (allSessions) {
|
|
389
|
+
warnings.push(msg('export.attachmentsAllSessions', {
|
|
390
|
+
count: String(keep.size),
|
|
391
|
+
total: String(present.size),
|
|
392
|
+
missing: String(missing),
|
|
393
|
+
}));
|
|
394
|
+
}
|
|
395
|
+
return { keep, warnings };
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* 预览富化(G5):给附件单元补「哪些会话用过它」,选择器据此把附件挂到会话行下面。
|
|
399
|
+
*
|
|
400
|
+
* 返回 `null` = 这次算不出归属(日志读不出/解不开、条数超上限)—— 界面按扁平渲染,
|
|
401
|
+
* **绝不猜归属**:把附件挂错会话,用户会以为「这个包里有那张图」而实际没有。
|
|
402
|
+
* 未变化的单元原样返回(缺字段 ≠ 空数组:没有归属就是不知道,不是「属于零条会话」)。
|
|
403
|
+
*/
|
|
404
|
+
async enrichUnits(ctx, units) {
|
|
405
|
+
if (units.length === 0)
|
|
406
|
+
return [...units];
|
|
407
|
+
let logs;
|
|
408
|
+
try {
|
|
409
|
+
const listing = await listFilesDetailed(ctx.fs, 'sessions');
|
|
410
|
+
logs = listing.paths.filter((rel) => isSessionLogFileName(toPosixRel(rel).split('/').pop() ?? ''));
|
|
411
|
+
}
|
|
412
|
+
catch {
|
|
413
|
+
return null;
|
|
414
|
+
}
|
|
415
|
+
const keyed = logs.filter((rel) => unitKeyOfLog(toPosixRel(rel)) !== null);
|
|
416
|
+
if (keyed.length === 0)
|
|
417
|
+
return [...units];
|
|
418
|
+
if (keyed.length > ENRICH_MAX_LOGS)
|
|
419
|
+
return null;
|
|
420
|
+
const scanned = await loadRefIndex(ctx, keyed);
|
|
421
|
+
if (!scanned.ok)
|
|
422
|
+
return null;
|
|
423
|
+
return units.map((unit) => {
|
|
424
|
+
// 单元 id = attachments:<attachmentId>;剥前缀查索引(形状不符 = 不是附件对象,原样返回)
|
|
425
|
+
const ref = unit.id.startsWith('attachments:') ? unit.id.slice('attachments:'.length) : unit.id;
|
|
426
|
+
const entry = scanned.index.get(ref);
|
|
427
|
+
if (entry === undefined)
|
|
428
|
+
return unit;
|
|
429
|
+
const shortId = shortAttachmentId(ref);
|
|
430
|
+
/**
|
|
431
|
+
* 展示名三级回落:**真名** → MIME → 短哈希。
|
|
432
|
+
*
|
|
433
|
+
* 真名是用户唯一能认出来的东西(`P-01-after.png` 比 `image/png` 有用得多),
|
|
434
|
+
* 而 64 位哈希对谁都没有信息量。三级都是日志里的既有事实,绝不编「附件 1」这类序号 ——
|
|
435
|
+
* 序号会随勾选范围变化,用户二次打开时对不上号。
|
|
436
|
+
*/
|
|
437
|
+
const displayName = entry.name ?? entry.mediaType ?? shortId;
|
|
438
|
+
return {
|
|
439
|
+
...unit,
|
|
440
|
+
label: displayName,
|
|
441
|
+
// label 已经是短哈希时不必把同一个值再写一遍
|
|
442
|
+
...(displayName !== shortId && unit.detail === undefined ? { detail: shortId } : {}),
|
|
443
|
+
...(entry.units.length > 0 ? { parentUnitIds: entry.units.map((key) => `sessions:${key}`) } : {}),
|
|
444
|
+
};
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
//# sourceMappingURL=attachments.js.map
|
package/lib/adapters/index.d.ts
CHANGED
|
@@ -53,6 +53,7 @@ export { WorkspacesAdapter } from './workspaces.ts';
|
|
|
53
53
|
export { CredentialsAdapter, defaultCredentialRefs } from './credentials.ts';
|
|
54
54
|
export { PluginFilesAdapter, DEFAULT_PLUGIN_FILE_WHITELIST } from './plugin-files.ts';
|
|
55
55
|
export { SessionsAdapter } from './sessions.ts';
|
|
56
|
+
export { AttachmentsAdapter } from './attachments.ts';
|
|
56
57
|
export { SelfAdapter, SELF_CONFIG_FILES } from './self.ts';
|
|
57
58
|
export { PluginDataAdapter, type PluginDataAdapterOptions, type PluginDataEntry } from './plugin-data.ts';
|
|
58
59
|
export { FileCollectionAdapter } from './file-collection.ts';
|
package/lib/adapters/index.js
CHANGED
|
@@ -11,6 +11,7 @@ import { WorkspacesAdapter } from './workspaces.js';
|
|
|
11
11
|
import { CredentialsAdapter } from './credentials.js';
|
|
12
12
|
import { PluginFilesAdapter } from './plugin-files.js';
|
|
13
13
|
import { SessionsAdapter } from './sessions.js';
|
|
14
|
+
import { AttachmentsAdapter } from './attachments.js';
|
|
14
15
|
import { SelfAdapter } from './self.js';
|
|
15
16
|
import { PluginDataAdapter } from './plugin-data.js';
|
|
16
17
|
/** 组装默认 adapter 列表(pluginFiles/sessions 恒挂载但 defaultIncluded=false,用户勾选才导出) */
|
|
@@ -37,8 +38,12 @@ export function createAdapters(options = {}) {
|
|
|
37
38
|
if (options.selfDir !== '') {
|
|
38
39
|
adapters.push(new SelfAdapter(options.selfDir ?? 'dsh-config-manager'));
|
|
39
40
|
}
|
|
40
|
-
if (options.includeSessions)
|
|
41
|
+
if (options.includeSessions) {
|
|
41
42
|
adapters.push(new SessionsAdapter());
|
|
43
|
+
// P1:附件必须排在 sessions **之后** —— 它的收集面由「本次选中的会话」决定,
|
|
44
|
+
// 顺序反了不会报错,只会静默把所有会话都当成「未收窄」的全集。
|
|
45
|
+
adapters.push(new AttachmentsAdapter());
|
|
46
|
+
}
|
|
42
47
|
return adapters;
|
|
43
48
|
}
|
|
44
49
|
export { SettingsAdapter } from './settings.js';
|
|
@@ -54,6 +59,7 @@ export { WorkspacesAdapter } from './workspaces.js';
|
|
|
54
59
|
export { CredentialsAdapter, defaultCredentialRefs } from './credentials.js';
|
|
55
60
|
export { PluginFilesAdapter, DEFAULT_PLUGIN_FILE_WHITELIST } from './plugin-files.js';
|
|
56
61
|
export { SessionsAdapter } from './sessions.js';
|
|
62
|
+
export { AttachmentsAdapter } from './attachments.js';
|
|
57
63
|
export { SelfAdapter, SELF_CONFIG_FILES } from './self.js';
|
|
58
64
|
export { PluginDataAdapter } from './plugin-data.js';
|
|
59
65
|
export { FileCollectionAdapter } from './file-collection.js';
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import { FileCollectionAdapter } from './file-collection.ts';
|
|
11
11
|
import './messages.ts';
|
|
12
|
-
import type { ApplyResult, ExportOptions, ExportSection, HostContext, ImportContext, PlanItem } from '../core/types.ts';
|
|
12
|
+
import type { ApplyResult, ExportOptions, ExportSection, HostContext, ImportContext, PlanItem, SessionCompareSummary } from '../core/types.ts';
|
|
13
13
|
import type { FilesSection } from '../schema/types.ts';
|
|
14
14
|
export declare class SessionsAdapter extends FileCollectionAdapter {
|
|
15
15
|
readonly id: 'sessions';
|
|
@@ -135,6 +135,23 @@ export declare class SessionsAdapter extends FileCollectionAdapter {
|
|
|
135
135
|
* 挂在本地父对话之下 —— 无脑告警会把正常情况说成「看不到」,比不告警更糟。
|
|
136
136
|
*/
|
|
137
137
|
private orphanSubagentWarnings;
|
|
138
|
+
/** 本机同名会话目录(两种命名形态都试);都没有 → null(= 包内是新增会话)。 */
|
|
139
|
+
private localSessionDir;
|
|
140
|
+
/**
|
|
141
|
+
* P3「逐条会话比对」:包内会话 vs **本机同名会话**(只读,零写入)。
|
|
142
|
+
*
|
|
143
|
+
* 判据(详见 core/session-compare.ts 文件头):同名 = 同一 projectKey + 同一会话目录
|
|
144
|
+
* (两种目录命名形态都试);按**日志文件名**配对比较事件流,同一单元多份日志取最严重结论。
|
|
145
|
+
*
|
|
146
|
+
* 三条不猜的边界:
|
|
147
|
+
* - 包内日志解不出(容器损坏 / 缺 zstd 能力)→ 该单元不比对,绝不当作「新增」;
|
|
148
|
+
* - 本机目录在、但没有任何可配对的日志 → verdict=`unreadable`(不是 new);
|
|
149
|
+
* - 本机读失败 → `unreadable`;只有「目录形态都探不到」才是 `new`。
|
|
150
|
+
*
|
|
151
|
+
* 预算:条数与字节双上限(超限的单元计入 skipped,报告必须写明「另有 N 条未比对」)。
|
|
152
|
+
* 附件引用统计覆盖**全部**可解码单元(缺口数不能因预算被截断而少报)。
|
|
153
|
+
*/
|
|
154
|
+
compareSessions(ctx: HostContext, data: FilesSection): Promise<SessionCompareSummary | null>;
|
|
138
155
|
applyItem(item: PlanItem, ctx: ImportContext): Promise<ApplyResult>;
|
|
139
156
|
/**
|
|
140
157
|
* 目标机上是否存在该会话单元的**任何文件**(ui-F1 的判据)。
|