@xsok/dsh-enhanced 0.0.0-stage → 0.2.2-3
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.md +149 -2
- package/client.js +2866 -0
- package/cordis.patch.yml +4 -0
- package/host/engine/archive.js +134 -0
- package/host/engine/atomic.js +27 -0
- package/host/engine/attachments.js +102 -0
- package/host/engine/config.js +214 -0
- package/host/engine/index.js +323 -0
- package/host/engine/ledger.js +108 -0
- package/host/engine/paths.js +81 -0
- package/host/engine/pluginlist.js +81 -0
- package/host/engine/projcache.js +165 -0
- package/host/engine/prune.js +187 -0
- package/host/engine/scan.js +177 -0
- package/host/engine/session.js +138 -0
- package/host/engine/sessions.js +346 -0
- package/host/engine/workspace.js +223 -0
- package/host/engine/zstd.js +91 -0
- package/host/envcheck.js +288 -0
- package/host/mirror.js +474 -0
- package/host/move.js +196 -0
- package/host/overwrite.js +305 -0
- package/host/restart.js +158 -0
- package/host/schedule.js +257 -0
- package/host/search.js +182 -0
- package/host/sync.js +593 -0
- package/host/trash.js +738 -0
- package/host/trashsync.js +204 -0
- package/host/webdav.js +429 -0
- package/icon.svg +1 -0
- package/index.js +312 -0
- package/locale/en.json +68 -0
- package/locale/zh.json +68 -0
- package/package.json +70 -4
package/host/envcheck.js
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Environment self-check for DSH增强.
|
|
3
|
+
*
|
|
4
|
+
* Everything the plugin relies on but does not own is probed here: host services,
|
|
5
|
+
* the Node features the engine needs, and the on-disk formats it reads and writes.
|
|
6
|
+
* A failed check never throws; it is reported so the user can export a report.
|
|
7
|
+
*/
|
|
8
|
+
import { promises as fsp } from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import os from 'node:os';
|
|
11
|
+
import zlib from 'node:zlib';
|
|
12
|
+
import { scanFrames } from './engine/zstd.js';
|
|
13
|
+
import { scanSessions, projectKey } from './engine/scan.js';
|
|
14
|
+
|
|
15
|
+
const PLUGIN_VERSION = '0.2.2-3';
|
|
16
|
+
|
|
17
|
+
async function readJson(file) {
|
|
18
|
+
try {
|
|
19
|
+
return JSON.parse(await fsp.readFile(file, 'utf8'));
|
|
20
|
+
} catch {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function exists(target) {
|
|
26
|
+
try {
|
|
27
|
+
await fsp.access(target);
|
|
28
|
+
return true;
|
|
29
|
+
} catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Best-effort host application version: env first, then the packaged manifest. */
|
|
35
|
+
export async function appVersion() {
|
|
36
|
+
const candidates = [
|
|
37
|
+
process.env.DSH_CLIENT_VERSION,
|
|
38
|
+
process.env.DSH_APP_VERSION,
|
|
39
|
+
process.env.DSH_VERSION,
|
|
40
|
+
];
|
|
41
|
+
for (const value of candidates) {
|
|
42
|
+
if (typeof value === 'string' && value.trim() !== '') return { version: value.trim(), source: 'env' };
|
|
43
|
+
}
|
|
44
|
+
const resources = [
|
|
45
|
+
typeof process.resourcesPath === 'string' ? process.resourcesPath : null,
|
|
46
|
+
process.execPath === undefined ? null : path.join(path.dirname(process.execPath), 'resources'),
|
|
47
|
+
].filter(Boolean);
|
|
48
|
+
for (const base of resources) {
|
|
49
|
+
for (const entry of ['app.asar', 'app']) {
|
|
50
|
+
const manifest = path.join(base, entry, 'package.json');
|
|
51
|
+
const doc = await readJson(manifest);
|
|
52
|
+
if (typeof doc?.version === 'string' && doc.version !== '') {
|
|
53
|
+
return { version: doc.version, source: manifest };
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return { version: null, source: 'unknown' };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function check(id, label, ok, detail, severity = 'high') {
|
|
61
|
+
return { id, label, ok, detail, severity };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Run every probe.
|
|
66
|
+
* @returns `{ ok, checks, appVersion, pluginVersion, checkedAt }`.
|
|
67
|
+
*/
|
|
68
|
+
export async function runEnvironmentCheck(deps) {
|
|
69
|
+
const { paths, ctx } = deps;
|
|
70
|
+
const checks = [];
|
|
71
|
+
|
|
72
|
+
// 1. host services the plugin injects
|
|
73
|
+
const services = ['webServer', 'connection', 'sessionPersistence', 'workspaceRegistry'];
|
|
74
|
+
for (const key of services) {
|
|
75
|
+
const service = ctx?.get?.(key);
|
|
76
|
+
checks.push(check(`service:${key}`, `宿主服务 ${key}`, service !== undefined, service === undefined ? '未注册(插件可能无法正常工作)' : '可用'));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const registry = ctx?.get?.('workspaceRegistry');
|
|
80
|
+
const registryMethods = ['get', 'list', 'resolveByPath', 'unpinSession', 'unarchiveSession'];
|
|
81
|
+
const missingRegistry = registry === undefined ? registryMethods : registryMethods.filter((name) => typeof registry[name] !== 'function');
|
|
82
|
+
checks.push(check('methods:workspaceRegistry', '工作区注册表方法完整', missingRegistry.length === 0, missingRegistry.length === 0 ? '全部存在' : `缺少:${missingRegistry.join('、')}`));
|
|
83
|
+
|
|
84
|
+
// Membership edits live on one Workspace instance, not on the registry.
|
|
85
|
+
const workspaceMethods = ['attachSession', 'detachSession', 'insertSessionBefore'];
|
|
86
|
+
let sampleWorkspace;
|
|
87
|
+
try {
|
|
88
|
+
sampleWorkspace = registry?.list?.()[0];
|
|
89
|
+
} catch {
|
|
90
|
+
sampleWorkspace = undefined;
|
|
91
|
+
}
|
|
92
|
+
if (sampleWorkspace === undefined) {
|
|
93
|
+
checks.push(check('methods:workspace', '工作区成员接口', true, '暂无工作区,跳过该项', 'low'));
|
|
94
|
+
} else {
|
|
95
|
+
const missingWorkspace = workspaceMethods.filter((name) => typeof sampleWorkspace[name] !== 'function');
|
|
96
|
+
checks.push(check('methods:workspace', '工作区成员接口', missingWorkspace.length === 0, missingWorkspace.length === 0 ? '全部存在' : `缺少:${missingWorkspace.join('、')}`));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const agents = ctx?.get?.('agents');
|
|
100
|
+
const agentSample = Array.isArray(agents?.list?.()) ? agents.list()[0] : undefined;
|
|
101
|
+
checks.push(check(
|
|
102
|
+
'methods:agents',
|
|
103
|
+
'会话状态接口可用',
|
|
104
|
+
typeof agents?.list === 'function' && (agentSample === undefined || 'status' in agentSample),
|
|
105
|
+
typeof agents?.list === 'function' ? '可读取 status' : '缺少 agents.list',
|
|
106
|
+
'medium',
|
|
107
|
+
));
|
|
108
|
+
|
|
109
|
+
// 2. runtime features the engine needs
|
|
110
|
+
checks.push(check(
|
|
111
|
+
'node:zstd',
|
|
112
|
+
'Node zstd 能力',
|
|
113
|
+
typeof zlib.zstdCompressSync === 'function' && typeof zlib.zstdDecompressSync === 'function',
|
|
114
|
+
typeof zlib.zstdCompressSync === 'function' ? '可用' : '当前 Node 缺少 zstd,会话读写不可用',
|
|
115
|
+
));
|
|
116
|
+
checks.push(check('node:fetch', 'Node fetch 可用', typeof fetch === 'function', typeof fetch === 'function' ? '可用' : '缺少 fetch,WebDAV 不可用'));
|
|
117
|
+
|
|
118
|
+
// 3. session format
|
|
119
|
+
const sessionsRoot = path.join(paths.home, 'sessions');
|
|
120
|
+
const sessions = await scanSessions(sessionsRoot);
|
|
121
|
+
if (sessions.size === 0) {
|
|
122
|
+
checks.push(check('format:sessions', '会话文件格式', false, '未找到任何会话,无法验证格式', 'medium'));
|
|
123
|
+
} else {
|
|
124
|
+
const sample = sessions.values().next().value;
|
|
125
|
+
let detail = '无法读取样本会话';
|
|
126
|
+
let ok = false;
|
|
127
|
+
try {
|
|
128
|
+
const buffer = await fsp.readFile(sample.file);
|
|
129
|
+
const frames = scanFrames(buffer);
|
|
130
|
+
const text = frames.length === 0 ? '' : zlib.zstdDecompressSync(buffer.subarray(frames[0].start, frames[0].end)).toString('utf8');
|
|
131
|
+
const header = text.split('\n').find((line) => line.trim() !== '');
|
|
132
|
+
const parsed = header === undefined ? undefined : JSON.parse(header);
|
|
133
|
+
ok = frames.length > 0 && parsed?.type === 'session' && typeof parsed.id === 'string' && typeof parsed.cwd === 'string';
|
|
134
|
+
detail = ok
|
|
135
|
+
? `帧数 ${String(frames.length)},首行字段齐全`
|
|
136
|
+
: `帧数 ${String(frames.length)},首行 ${ok ? '正常' : '缺少 type/id/cwd'}`;
|
|
137
|
+
} catch (error) {
|
|
138
|
+
detail = `读取失败:${String(error?.message ?? error)}`;
|
|
139
|
+
}
|
|
140
|
+
checks.push(check('format:sessions', '会话文件格式', ok, detail));
|
|
141
|
+
|
|
142
|
+
// projectKey must still match the folder the host chose
|
|
143
|
+
const expected = projectKey(sample.projectKey.startsWith('--') ? sample.projectKey : '');
|
|
144
|
+
const dirName = path.basename(path.dirname(path.dirname(sample.file)));
|
|
145
|
+
checks.push(check(
|
|
146
|
+
'format:projectKey',
|
|
147
|
+
'项目目录命名规则',
|
|
148
|
+
dirName === sample.projectKey || expected === '',
|
|
149
|
+
`目录 ${dirName},记录 ${sample.projectKey}`,
|
|
150
|
+
'medium',
|
|
151
|
+
));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// 4. workspace.json schema
|
|
155
|
+
const workspaceFile = path.join(paths.home, 'storages', 'workspace.json');
|
|
156
|
+
const workspaceDoc = await readJson(workspaceFile);
|
|
157
|
+
if (workspaceDoc === undefined) {
|
|
158
|
+
checks.push(check('format:workspace', 'workspace.json 结构', false, '文件缺失或无法解析', 'medium'));
|
|
159
|
+
} else {
|
|
160
|
+
const unitVersion = Number(workspaceDoc.unit?.version);
|
|
161
|
+
const table = workspaceDoc.tables?.workspaces;
|
|
162
|
+
const records = table !== null && typeof table === 'object' ? Object.values(table) : [];
|
|
163
|
+
const badPaths = records.filter((record) => typeof record?.path !== 'string' || record.path === '').length;
|
|
164
|
+
checks.push(check(
|
|
165
|
+
'format:workspace',
|
|
166
|
+
'workspace.json 结构',
|
|
167
|
+
records.length > 0 && badPaths === 0 && Number.isFinite(unitVersion),
|
|
168
|
+
`版本 ${String(unitVersion)},分组 ${String(records.length)} 个${badPaths > 0 ? `,其中 ${String(badPaths)} 个 path 非法` : ''}`,
|
|
169
|
+
));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// 5. title cache shape (optional)
|
|
173
|
+
const shardDir = path.join(paths.home, 'storages', 'session_projcache', 'sessions');
|
|
174
|
+
if (await exists(shardDir)) {
|
|
175
|
+
let entries = [];
|
|
176
|
+
try {
|
|
177
|
+
entries = (await fsp.readdir(shardDir)).filter((name) => name.endsWith('.json')).slice(0, 3);
|
|
178
|
+
} catch {
|
|
179
|
+
entries = [];
|
|
180
|
+
}
|
|
181
|
+
let ok = entries.length === 0;
|
|
182
|
+
let detail = '暂无缓存分片';
|
|
183
|
+
for (const name of entries) {
|
|
184
|
+
const doc = await readJson(path.join(shardDir, name));
|
|
185
|
+
const rows = doc?.record?.rows ?? doc?.rows;
|
|
186
|
+
if (rows !== undefined && rows !== null) {
|
|
187
|
+
ok = true;
|
|
188
|
+
detail = 'record.rows 结构可读';
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
checks.push(check('format:titles', '会话标题缓存结构', ok, detail, 'low'));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// 6. attachments layout (optional)
|
|
196
|
+
const attachmentsRoot = path.join(paths.home, 'attachments', 'v1');
|
|
197
|
+
checks.push(check(
|
|
198
|
+
'format:attachments',
|
|
199
|
+
'附件目录结构',
|
|
200
|
+
await exists(attachmentsRoot),
|
|
201
|
+
(await exists(path.join(attachmentsRoot, 'objects'))) ? 'objects 目录存在' : '尚无附件对象',
|
|
202
|
+
'low',
|
|
203
|
+
));
|
|
204
|
+
|
|
205
|
+
// 7. profile config used by the sync
|
|
206
|
+
const profile = process.env.DSH_PROFILE ?? 'desktop';
|
|
207
|
+
const patchFile = path.join(paths.home, 'profiles', profile, 'cordis.patch.yml');
|
|
208
|
+
checks.push(check('file:profile', '当前 profile 补丁文件', await exists(patchFile), patchFile, 'low'));
|
|
209
|
+
|
|
210
|
+
// 8. data directory writable
|
|
211
|
+
let writable = false;
|
|
212
|
+
try {
|
|
213
|
+
const probe = path.join(paths.base, `.env-check-${String(process.pid)}.tmp`);
|
|
214
|
+
await fsp.writeFile(probe, 'ok', 'utf8');
|
|
215
|
+
await fsp.rm(probe, { force: true });
|
|
216
|
+
writable = true;
|
|
217
|
+
} catch {
|
|
218
|
+
writable = false;
|
|
219
|
+
}
|
|
220
|
+
checks.push(check('fs:data', '插件数据目录可写', writable, paths.base));
|
|
221
|
+
|
|
222
|
+
// 9. host version detectable
|
|
223
|
+
const version = await appVersion();
|
|
224
|
+
checks.push(check('app:version', '主程序版本可识别', version.version !== null, version.version ?? '无法读取(版本变化提醒不可用,其余检测不受影响)', 'low'));
|
|
225
|
+
|
|
226
|
+
const failed = checks.filter((item) => item.ok !== true);
|
|
227
|
+
return {
|
|
228
|
+
ok: failed.length === 0,
|
|
229
|
+
checkedAt: new Date().toISOString(),
|
|
230
|
+
pluginVersion: PLUGIN_VERSION,
|
|
231
|
+
appVersion: version.version,
|
|
232
|
+
appVersionSource: version.source,
|
|
233
|
+
checks,
|
|
234
|
+
failedCount: failed.length,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Where the desktop report is written. */
|
|
239
|
+
function desktopDirectory() {
|
|
240
|
+
const desktop = path.join(os.homedir(), 'Desktop');
|
|
241
|
+
return desktop;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** Where the last-seen application version is remembered. */
|
|
245
|
+
function recordFile(paths) {
|
|
246
|
+
return path.join(paths.base, 'env-check.json');
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Read the remembered version. */
|
|
250
|
+
export async function readEnvRecord(paths) {
|
|
251
|
+
return (await readJson(recordFile(paths))) ?? {};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Remember the current application version. */
|
|
255
|
+
export async function writeEnvRecord(paths, patch) {
|
|
256
|
+
const current = await readEnvRecord(paths);
|
|
257
|
+
await fsp.writeFile(recordFile(paths), `${JSON.stringify({ ...current, ...patch }, null, 2)}\n`, 'utf8');
|
|
258
|
+
return { ...current, ...patch };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** Write the report and return its path. */
|
|
262
|
+
export async function writeEnvironmentReport(deps, result) {
|
|
263
|
+
const target = desktopDirectory();
|
|
264
|
+
const stamp = new Date().toISOString().replace(/[:.]/gu, '-').slice(0, 19);
|
|
265
|
+
const file = path.join(target, `DSH增强-环境检测-${stamp}.md`);
|
|
266
|
+
const lines = [
|
|
267
|
+
'# DSH增强 · 环境检测报告',
|
|
268
|
+
'',
|
|
269
|
+
`- 检测时间:${result.checkedAt}`,
|
|
270
|
+
`- 插件版本:V${result.pluginVersion}`,
|
|
271
|
+
`- 主程序版本:${result.appVersion ?? '未知'}`,
|
|
272
|
+
`- 结论:${result.ok ? '全部通过' : `发现 ${String(result.failedCount)} 项风险`}`,
|
|
273
|
+
'',
|
|
274
|
+
'## 检测明细',
|
|
275
|
+
'',
|
|
276
|
+
'| 结果 | 项目 | 说明 | 严重度 |',
|
|
277
|
+
'| --- | --- | --- | --- |',
|
|
278
|
+
...result.checks.map((item) => `| ${item.ok ? '通过' : '**异常**'} | ${item.label} | ${String(item.detail ?? '').replace(/\|/gu, '\\|')} | ${item.severity} |`),
|
|
279
|
+
'',
|
|
280
|
+
'## 说明',
|
|
281
|
+
'',
|
|
282
|
+
'- 异常项按严重度排序观察:high 表示可能直接影响会话数据,medium 表示部分功能失效,low 表示可忽略。',
|
|
283
|
+
'- 若“会话文件格式”或“workspace.json 结构”异常,建议升级插件版本,而不要继续同步。',
|
|
284
|
+
];
|
|
285
|
+
await fsp.mkdir(target, { recursive: true }).catch(() => {});
|
|
286
|
+
await fsp.writeFile(file, `${lines.join('\n')}\n`, 'utf8');
|
|
287
|
+
return { ok: true, path: file };
|
|
288
|
+
}
|